SlideShare a Scribd company logo
Ruby!
What’s a Metaclass?
      Tommy xiao’s assemble
        twitter: @xds2000

understand how Ruby’s metaclass works
have no idea?

According to wikipedia:

  In object-oriented programming, a
  metaclass is a class whose instances are
  classes. Just as an ordinary class defines
  the behavior of certain objects, a
  metaclass defines the behavior of certain
  classes and their instances.
Get two things

(1) instances of metaclasses are classes

(2) the metaclass defines the behavior of a class.
So Singleton classes
aren’t Metaclasses?
In general, instances of singleton classes are
regular objects, not classes. Singleton classes
define the behavior of regular objects, not
class objects.
Too Complicated?
Not Understood?

In Ruby:
Not all singleton classes are metaclasses. Only
singleton classes of classes are metaclasses.
And then only weak, partial metaclasses.
in real coding,i will see..
eval instance_eval class_eval
class << self; self; end

   Shit! i have no idea. :-(
use code snippets
Foo = Class.new
Foo.class_eval do
  def bar
    "bar"
  end
end
Foo.instance_eval   do
  def baz
    "baz"
  end
end
Foo.bar       #=>   undefined method ‘bar’ for Foo:Class
Foo.new.bar   #=>   "bar"
Foo.baz       #=>   "baz"
Foo.new.baz   #=>   undefined method ‘baz’ for #<Foo:0x7dce8>
matz = Object.new
 def matz.speak
   "Place your burden to machine's
 shoulders"
 end

What’s going on here is that we’re adding the speak
method to matz’s metaclass, and the matz object
inherits from its metaclass and then Object.
Key Concepts
the metaclass is invisible in Ruby
atz = Object.new
def matz.speak
  "Place your burden to machine's
shoulders"
end
 
matz.class #=> Object

In fact, matz’s “class” is its invisible metaclass.
Key Concepts

get access to the metaclass

metaclass = class << matz; self; end
metaclass.instance_methods.grep(/speak/) #=> ["speak"]




it seems as though there are so many rules.
in fact only single concept



control over the self in a
given part of the code.
use sample code to prove our views
# use self.def define class method
Dragon.class_eval do # use instance_eval
is same effect(why?)
  def self.foo
    puts "bar"
  end
end
Dragon.foo # bar
because self is which evaluates to
receiver(Dragon)
Dragon.instance_eval do
  define_method("foo2") { puts "bar" }
end
Dragon.foo2 rescue puts "fails" #
undefined method(why?)

implicit self, which evaluates to receiver(Dragon)
so here actually define instance method.
metaclass = (class << Dragon; self; end)
metaclass.instance_eval do # use
class_eval is same effect(why?)
    define_method("foo") { puts "bar" }
end
Dragon.foo


because self is which evaluates to
receiver(metaclass)
Dragon.class.instance_eval do # use
class_eval is ok
    define_method("foo") { puts "bar" }
end
Dragon.foo # bar
String.foo # bar, all class pollut!! bad
idea!!

because self is which evaluates to
receiver(Class)
refs
http://onestepback.org/index.cgi/Tech/Ruby/
Metaclasses.red
http://gist.github.com/366463
http://gist.github.com/366372
http://www.khelll.com/blog/ruby/ruby-reflection/
http://blog.jayfields.com/2007/03/ruby-
instanceeval-and-classeval-method.html
http://yehudakatz.com/2009/11/15/
metaprogramming-in-ruby-its-all-about-the-self/
End!

More Related Content

PPTX
Ruby object model - Understanding of object play role for ruby
PDF
Not So Foreign Functions
PPTX
adnoto
PPTX
Interesting Facts About Javascript
ODP
Intro Ruby Classes Part I
PPTX
Javascript Objects Deep Dive
PPTX
Javascript Prototypal Inheritance - Big Picture
ODP
Constructors, Intro to Ruby Classes Part II
Ruby object model - Understanding of object play role for ruby
Not So Foreign Functions
adnoto
Interesting Facts About Javascript
Intro Ruby Classes Part I
Javascript Objects Deep Dive
Javascript Prototypal Inheritance - Big Picture
Constructors, Intro to Ruby Classes Part II

What's hot (20)

DOC
Java classes and objects interview questions
PPTX
Paca oops slid
PPTX
Object oriented programming in JavaScript
PPTX
WHAT IS ABSTRACTION IN JAVA
PDF
How prototype works in java script?
DOCX
Java interview questions and answers for cognizant By Data Council Pune
PPTX
Chapter 8 java
PPT
Ruby Metaprogramming
PDF
Lets talk-about-js
PDF
The prototype property
PPTX
Advanced Python : Static and Class Methods
DOCX
Java Core Parctical
PDF
Metaprogramming
PDF
Java Basics Presentation
PDF
Metaprogramming Rails
PDF
Design pattern is_everywhere_by_saurabh_sharma
PPTX
Java basics
TXT
Java interview
PDF
Metaprogramming in Ruby
PPTX
Java classes and objects interview questions
Paca oops slid
Object oriented programming in JavaScript
WHAT IS ABSTRACTION IN JAVA
How prototype works in java script?
Java interview questions and answers for cognizant By Data Council Pune
Chapter 8 java
Ruby Metaprogramming
Lets talk-about-js
The prototype property
Advanced Python : Static and Class Methods
Java Core Parctical
Metaprogramming
Java Basics Presentation
Metaprogramming Rails
Design pattern is_everywhere_by_saurabh_sharma
Java basics
Java interview
Metaprogramming in Ruby
Ad

Viewers also liked (7)

KEY
Pragmatic blocks
PDF
Understanding Metaclasses
PDF
The meta of Meta-object Architectures
PPT
Stoop 304-metaclasses
PPTX
April iOS Meetup - UIAppearance Presentation
PDF
Objective runtime
PDF
Objective C for Samurais
Pragmatic blocks
Understanding Metaclasses
The meta of Meta-object Architectures
Stoop 304-metaclasses
April iOS Meetup - UIAppearance Presentation
Objective runtime
Objective C for Samurais
Ad

Similar to Ruby's metaclass (20)

PDF
Metaprogramming ruby
PPTX
The Black Magic of Ruby Metaprogramming
PDF
The Ruby Object Model by Rafael Magana
KEY
Metaprogramming Primer (Part 1)
PPTX
Deciphering the Ruby Object Model
KEY
Ruby objects
PDF
Ruby Metaprogramming - OSCON 2008
PPTX
Ruby :: Training 1
PDF
Ruby object model at the Ruby drink-up of Sophia, January 2013
PDF
Metaprogramming + Ds Ls
PDF
Ruby basics
PDF
Ruby Metaprogramming
PPTX
Ruby object model
PDF
Ruby seen from a C# developer
PDF
Ruby seen by a C# developer
PDF
Ruby Metaprogramming 08
PPTX
Metaprogramming in Ruby
PPTX
Introduction to Ruby’s Reflection API
PDF
Reflection in Ruby
PDF
Metaprogramming
Metaprogramming ruby
The Black Magic of Ruby Metaprogramming
The Ruby Object Model by Rafael Magana
Metaprogramming Primer (Part 1)
Deciphering the Ruby Object Model
Ruby objects
Ruby Metaprogramming - OSCON 2008
Ruby :: Training 1
Ruby object model at the Ruby drink-up of Sophia, January 2013
Metaprogramming + Ds Ls
Ruby basics
Ruby Metaprogramming
Ruby object model
Ruby seen from a C# developer
Ruby seen by a C# developer
Ruby Metaprogramming 08
Metaprogramming in Ruby
Introduction to Ruby’s Reflection API
Reflection in Ruby
Metaprogramming

Recently uploaded (20)

PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Cloud computing and distributed systems.
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Approach and Philosophy of On baking technology
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Spectroscopy.pptx food analysis technology
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
KodekX | Application Modernization Development
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Empathic Computing: Creating Shared Understanding
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
cuic standard and advanced reporting.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Mobile App Security Testing_ A Comprehensive Guide.pdf
Cloud computing and distributed systems.
Advanced methodologies resolving dimensionality complications for autism neur...
MYSQL Presentation for SQL database connectivity
Approach and Philosophy of On baking technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
Spectroscopy.pptx food analysis technology
Spectral efficient network and resource selection model in 5G networks
NewMind AI Weekly Chronicles - August'25 Week I
Understanding_Digital_Forensics_Presentation.pptx
KodekX | Application Modernization Development
Dropbox Q2 2025 Financial Results & Investor Presentation
sap open course for s4hana steps from ECC to s4
Encapsulation_ Review paper, used for researhc scholars
Chapter 3 Spatial Domain Image Processing.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Empathic Computing: Creating Shared Understanding
“AI and Expert System Decision Support & Business Intelligence Systems”
cuic standard and advanced reporting.pdf

Ruby's metaclass