Ruby Metaprogramming. The Ruby Object Model - Structure and Semantics. 2009-03-03 As part of my compiler project, one of my imminent decisions is what object model to use, and sine I like Ruby it seemed a good time to go through Ruby and look at the guts of the Ruby object model.
If you've dabbled in meta-programming etc. for Ruby this post probably doesn't contain much new stuff for you. If you're a beginner you may want to look at a tutorial instead. If you're somewhere in between, hopefully there may be some insights here and there - especially if you're interested specifically in how things work "under the hood" rather than just what is visible to Ruby.
The information in this post is based largely on the Ruby 1.8.x interpreter (you'll see it referred to as MRI as well - Matz Ruby interpreter - to distinguish it from other Ruby implementations), and we'll look at code fragments, as well as a diagram or two to illustrate. Suggested reading The Ruby object model can make you go insane. Ruby Objects Conceptually, Ruby objects consists of the following: Send #! The Ruby Object Model and Metaprogramming. Want to receive a weekly email containing the scoop on our new titles along with the occasional special offer?
Just click the button. (You can always unsubscribe later by editing your account information). Give us an email and a password (you can use the password later to log in and change your preferences). We'll send you a newsletter roughly once a week. Metaprogramming lets you program more expressively. Buy Now Download and watch DRM-free episodes when and where it's convenient for you You get Theora Ogg, iPod/iPhone 3, iPad/iPhone 4, and QuickTime formats. All the episodes in this series have been released. About this Screencast Initially, metaprogramming Ruby can seem really difficult. Well, the good news is that it really isn’t that complex.
It doesn’t matter if you’ve been programming Ruby for a month or for five years. Dave talks about the series in this free preview Contents and Extracts Audience These screencasts assume you have some basic Ruby knowledge. Ruby's Object Model: Metaprogramming and other Magic. Three implicit contexts in Ruby - 世界線航跡蔵. Yehuda Katz wrote an article about self and metaclass.
In the article he said that Person.instance_eval assigns Person's metaclass to self for a class Person. But this is obviously wrong. class Person; end Person.instance_eval{ p self } class Person; end Person.instance_eval{ p self } #=> Person As I mentioned in an old article, though I'm sorry about it is written in Japanese, Ruby always has three implicit contexts: self), so called `klass' and the constant definition point. Self self is the self which you know. P self class Foo def bar(a = (p self)) end end foo = Foo.new foo.bar class Foo class Baz < (p self; self) end end p self # displays "main" class Foo def bar(a = (p self)) end end foo = Foo.new foo.bar # displays "#<Foo:0x471004>" class Foo class Baz < (p self; self) # displays "Foo" end end On the top level, a special instance of Object named "main" is the self.
If you invoke a method without giving an explicit receiver, self will receive the invocation. so called `klass' Examples.