SlideShare ist ein Scribd-Unternehmen logo
1 von 17
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!

Weitere ähnliche Inhalte

Was ist angesagt?

Java classes and objects interview questions
Java classes and objects interview questionsJava classes and objects interview questions
Java classes and objects interview questions
Dhivyashree Selvarajtnkpm
 
Paca oops slid
Paca oops slidPaca oops slid
Paca oops slid
pacatarpit
 
Lets talk-about-js
Lets talk-about-jsLets talk-about-js
Lets talk-about-js
srnftw
 

Was ist angesagt? (20)

Java classes and objects interview questions
Java classes and objects interview questionsJava classes and objects interview questions
Java classes and objects interview questions
 
Paca oops slid
Paca oops slidPaca oops slid
Paca oops slid
 
Object oriented programming in JavaScript
Object oriented programming in JavaScriptObject oriented programming in JavaScript
Object oriented programming in JavaScript
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVA
 
How prototype works in java script?
How prototype works in java script?How prototype works in java script?
How prototype works in java script?
 
Java interview questions and answers for cognizant By Data Council Pune
Java interview questions and answers for cognizant By Data Council PuneJava interview questions and answers for cognizant By Data Council Pune
Java interview questions and answers for cognizant By Data Council Pune
 
Chapter 8 java
Chapter 8 javaChapter 8 java
Chapter 8 java
 
Ruby Metaprogramming
Ruby MetaprogrammingRuby Metaprogramming
Ruby Metaprogramming
 
Lets talk-about-js
Lets talk-about-jsLets talk-about-js
Lets talk-about-js
 
The prototype property
The prototype propertyThe prototype property
The prototype property
 
Advanced Python : Static and Class Methods
Advanced Python : Static and Class Methods Advanced Python : Static and Class Methods
Advanced Python : Static and Class Methods
 
Java Core Parctical
Java Core ParcticalJava Core Parctical
Java Core Parctical
 
Metaprogramming
MetaprogrammingMetaprogramming
Metaprogramming
 
Java Basics Presentation
Java Basics PresentationJava Basics Presentation
Java Basics Presentation
 
Metaprogramming Rails
Metaprogramming RailsMetaprogramming Rails
Metaprogramming Rails
 
Design pattern is_everywhere_by_saurabh_sharma
Design pattern is_everywhere_by_saurabh_sharmaDesign pattern is_everywhere_by_saurabh_sharma
Design pattern is_everywhere_by_saurabh_sharma
 
Java basics
Java basicsJava basics
Java basics
 
Java interview
Java interviewJava interview
Java interview
 
Metaprogramming in Ruby
Metaprogramming in RubyMetaprogramming in Ruby
Metaprogramming in Ruby
 
Module Ninja .JS
Module Ninja .JSModule Ninja .JS
Module Ninja .JS
 

Andere mochten auch

April iOS Meetup - UIAppearance Presentation
April iOS Meetup - UIAppearance PresentationApril iOS Meetup - UIAppearance Presentation
April iOS Meetup - UIAppearance Presentation
Long Weekend LLC
 

Andere mochten auch (7)

Pragmatic blocks
Pragmatic blocksPragmatic blocks
Pragmatic blocks
 
Understanding Metaclasses
Understanding MetaclassesUnderstanding Metaclasses
Understanding Metaclasses
 
The meta of Meta-object Architectures
The meta of Meta-object ArchitecturesThe meta of Meta-object Architectures
The meta of Meta-object Architectures
 
Stoop 304-metaclasses
Stoop 304-metaclassesStoop 304-metaclasses
Stoop 304-metaclasses
 
April iOS Meetup - UIAppearance Presentation
April iOS Meetup - UIAppearance PresentationApril iOS Meetup - UIAppearance Presentation
April iOS Meetup - UIAppearance Presentation
 
Objective runtime
Objective runtimeObjective runtime
Objective runtime
 
Objective C for Samurais
Objective C for SamuraisObjective C for Samurais
Objective C for Samurais
 

Ähnlich wie Ruby's metaclass

Aman kingrubyoo pnew
Aman kingrubyoo pnew Aman kingrubyoo pnew
Aman kingrubyoo pnew
ThoughtWorks
 

Ähnlich wie Ruby's metaclass (20)

Ruby Metaprogramming - OSCON 2008
Ruby Metaprogramming - OSCON 2008Ruby Metaprogramming - OSCON 2008
Ruby Metaprogramming - OSCON 2008
 
Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013
 
Java J2EE Interview Questions Part-1
Java J2EE Interview Questions Part-1Java J2EE Interview Questions Part-1
Java J2EE Interview Questions Part-1
 
Java J2EE Interview Questions Part-1
Java J2EE Interview Questions Part-1Java J2EE Interview Questions Part-1
Java J2EE Interview Questions Part-1
 
Deciphering the Ruby Object Model
Deciphering the Ruby Object ModelDeciphering the Ruby Object Model
Deciphering the Ruby Object Model
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
The Next Generation MOP, Jochen Theodorou, GR8Conf 2013
The Next Generation MOP, Jochen Theodorou, GR8Conf 2013 The Next Generation MOP, Jochen Theodorou, GR8Conf 2013
The Next Generation MOP, Jochen Theodorou, GR8Conf 2013
 
The Black Magic of Ruby Metaprogramming
The Black Magic of Ruby MetaprogrammingThe Black Magic of Ruby Metaprogramming
The Black Magic of Ruby Metaprogramming
 
01. design pattern
01. design pattern01. design pattern
01. design pattern
 
Ruby OOP: Objects over Classes
Ruby OOP: Objects over ClassesRuby OOP: Objects over Classes
Ruby OOP: Objects over Classes
 
Metaclasses – Python’s Object-Oriented Paradigm and Its Metaprogramming
Metaclasses – Python’s Object-Oriented Paradigm and Its MetaprogrammingMetaclasses – Python’s Object-Oriented Paradigm and Its Metaprogramming
Metaclasses – Python’s Object-Oriented Paradigm and Its Metaprogramming
 
Aman kingrubyoo pnew
Aman kingrubyoo pnew Aman kingrubyoo pnew
Aman kingrubyoo pnew
 
C#
C#C#
C#
 
Java questions for interview
Java questions for interviewJava questions for interview
Java questions for interview
 
Oop basic concepts
Oop basic conceptsOop basic concepts
Oop basic concepts
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
 
Ruby Interview Questions
Ruby Interview QuestionsRuby Interview Questions
Ruby Interview Questions
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
 
Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
 
20 most important java programming interview questions
20 most important java programming interview questions20 most important java programming interview questions
20 most important java programming interview questions
 

Kürzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Kürzlich hochgeladen (20)

CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

Ruby's metaclass

Hinweis der Redaktion