Download - Introduction to the Ruby Object Model
![Page 1: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/1.jpg)
Introduction to the
Ruby Object Model
![Page 2: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/2.jpg)
Ruby is an object
oriented language
![Page 3: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/3.jpg)
Primitives
182376
“hello, world”
[1, 2, 3]
Objects
String: “x”
object_id: 2817249
#length, #upcase, #..
Object
@name = “Peter”
@age = 30
object_id: 2132452
Array: [., .]
object_id: 2132452
#next, #size, #..
![Page 4: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/4.jpg)
An Object
#name=(name)
#name
#age=(age)
#age
@name = “Peter”
@age = 30
Data
Methods
class: User
object_id: 2132452
![Page 5: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/5.jpg)
In Ruby: everything (well, almost
everything) is an object:
• Classes
• “Primitive types”
• Modules (will be explained later on)
• Procs
![Page 6: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/6.jpg)
‘a’.upcase # => ‘A’
‘a’.class # => String
![Page 7: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/7.jpg)
10.class # => Fixnum
![Page 8: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/8.jpg)
class Fixnum
def +(other)
42
end
end
10 + 10 # => 42 (!)
![Page 9: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/9.jpg)
Fixnum.class # => Class
Class.class # => Class
![Page 10: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/10.jpg)
my_class = Class.new
my_obj = my_class.new
![Page 11: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/11.jpg)
The “Ruby Object Model”
![Page 12: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/12.jpg)
How Objects Are
Represented Internally
![Page 13: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/13.jpg)
struct RObject {
struct RBasic basic;
long numiv;
VALUE *ivptr;
struct st_table *iv_index_tbl;
};
Number of
instance variables
Instance variables array
Essentially a hash of
{ name -> index into ivptr }
@name = “Fred”
@age = 23
![Page 14: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/14.jpg)
struct RBasic {
VALUE flags;
VALUE klass;
};
Stores information like
whether the object is
frozen, tainted or others
Reference to
whatever the class
of the object is
![Page 15: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/15.jpg)
An object has:
• Klass (parent class)
• Flags (frozen? tainted? etc.)
• Instance variables
• Nothing else.
![Page 16: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/16.jpg)
How Classes Are
Represented Internally
![Page 17: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/17.jpg)
struct RClass {
struct RBasic basic;
rb_classext_t *ptr;
struct st_table *m_tbl;
struct st_table *iv_index_tbl;
};
struct rb_classext_struct {
VALUE super;
struct st_table *iv_tbl;
struct st_table *const_tbl;
};
Instance
variables
Constants
Reference
to superclass
Additional class
info
Methods
Instance
variables
![Page 18: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/18.jpg)
struct RClass {
VALUE flags;
VALUE klass;
VALUE super;
struct st_table *iv_tbl;
struct st_table *const_tbl;
struct st_table *m_tbl;
struct st_table *iv_index_tbl;
};
Methods
Reference
to superclass
Instance
variables
Constants
Instance
variables
![Page 19: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/19.jpg)
class Oban < Scotch
AGE = 14
@tasty = true
def tasty
Oban.instance_variable_get("@tasty")
end
end
![Page 20: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/20.jpg)
class Oban < Scotch
AGE = 14
@tasty = true
def tasty
Oban.instance_variable_get("@tasty")
end
end
basic.klass Class
ptr->super Scotch
iv_tbl { “@tasty” => true }
const_tbl { “AGE” = 14 }
m_tbl { “tasty” => … }
![Page 21: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/21.jpg)
Enough with the C
code!
![Page 22: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/22.jpg)
RObject and RClass
instance variables
flags
RObject
class reference
@age = 10
@name = “Peter”
instance variables
flags
RClass
class reference
methods table
reference to ‘superclass’
constants table
These three are extra to classes
![Page 23: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/23.jpg)
class User
def name=(name)
@name = name
end
def name
@name
end
end
![Page 24: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/24.jpg)
methods table
#name=
#name
class reference
User (RClass)
class instance variables
reference to ‘superclass’
class reference
Class (RClass)
class reference
Object (RClass)
![Page 25: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/25.jpg)
class User
def name=(name)
@name = name
end
def name
@name
end
end
me = User.new
![Page 26: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/26.jpg)
methods table
#name=
#name
User (RClass)class reference
RObject
instance variables
![Page 27: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/27.jpg)
methods table
#name=
#name
User (RClass)class reference
RObject
instance variables
@name => “Peter Cooper”
me.name = “Peter Cooper”
![Page 28: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/28.jpg)
class User
def status
:admin
end
end
![Page 29: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/29.jpg)
methods table
#name=
#name
#status
User (RClass)class reference
RObject
instance variables
@name => “Peter Cooper”
![Page 30: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/30.jpg)
me = User.new
you = User.new
def me.age
30
end
me.age # => ?
you.age # => ?
![Page 31: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/31.jpg)
me = User.new
you = User.new
def me.age
30
end
me.age # => 30
you.age # => NoMethodError
![Page 32: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/32.jpg)
methods table
#name=
#name
User (RClass)
class reference
RObject
Me
class reference
RObject
You
.. but where does
me.age go?
![Page 33: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/33.jpg)
methods table
#name=
#name
User (RClass)
class reference
RObject
Me
class reference
RObject
You
methods table
#age
*me (RClass)
singleton class
![Page 34: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/34.jpg)
class reference
RObject
Me
class reference
RObject
You
*me (RClass)
methods table
#age
superclass
User (RClass)
methods table
#name=
#name
![Page 35: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/35.jpg)
me.class # => User
![Page 36: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/36.jpg)
“Class Methods”
![Page 37: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/37.jpg)
class User
def self.plural_name
“users”
end
end
User.plural_name # => “users”
me.plural_name # => NoMethodError
you.plural_name # => NoMethodError
![Page 38: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/38.jpg)
methods table
#name=
#name
superclass
User (RClass)
klass
Class (RClass)
methods table
#plural_name
*User (RClass)
klass
Object (RClass)
![Page 39: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/39.jpg)
methods table
#name=
#name
superclass
User (RClass)
klass
Class (RClass)
methods table
#plural_name
*User (RClass)
klass
Object (RClass)Conclusion:
All methods in Ruby
are actually
instance methods!
![Page 40: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/40.jpg)
Modules
“Collection of methods
and constants”
![Page 41: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/41.jpg)
module MyModule
PI = 3.141592
def some_method
puts "Hello, world!"
end
end
MyModule::PI # => 3.141592 (yay!)
MyModule.some_method # => NoMethodError (eh!?)
![Page 42: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/42.jpg)
class User
include MyModule
end
me.some_method # => “Hello, world!”
Using modules as “mixins"
![Page 43: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/43.jpg)
class User
include MyModule
end
me.some_method # => “Hello, world!”
“Mixing in” functionality
into an existing class
Using modules as “mixins"
![Page 44: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/44.jpg)
module MyModule
def self.some_method
puts “Hello, world!”
end
end
MyModule.some_method # => “Hello, world!”
Using modules as function grouping in a
namespace (helper/utility classes)
![Page 45: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/45.jpg)
Math.cos(10)
![Page 46: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/46.jpg)
module NamingMethods
def name=(name)
@name = name
end
def name
@name
end
end
class User
include NamingMethods
end
me = User.new
me.name = “Fred”
p me.name
![Page 47: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/47.jpg)
module NamingMethods
def name=(name)
@name = name
end
def name
@name
end
end
class User
include NamingMethods
end
me = User.new
me.name = “Fred”
p me.name
Can anybody guess
what’s going on
behind the scenes?
![Page 48: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/48.jpg)
methods table
#name=
#name
superclass
User (RClass)
methods table
#name=
#name
#hello
NamingMethods (RModule)
Object (RClass)
![Page 49: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/49.jpg)
methods table
#name=
#name
superclass
User (RClass)
methods table
#name=
#name
#hello
NamingMethods (RModule)
Object (RClass)
NamingMethods (IClass)
methods table
superclass
![Page 50: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/50.jpg)
“extend” vs “include”
![Page 51: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/51.jpg)
module ClassMethods
def hello
42
end
end
class User
extend ClassMethods
end
p User.hello # => 42
![Page 52: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/52.jpg)
module SingletonMethods
def hello
42
end
end
str = “hello”
str.extend SingletonMethods
p str.hello # => 42
![Page 53: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/53.jpg)
module MyFunctionality
def instance_meth
puts “I’m an instance method”
end
def self.included(klass)
klass.extend ClassMethods
end
module ClassMethods
def class_meth
puts “I’m a class method”
end
end
end
class MyClass
include MyFunctionality
end
p MyClass.class_meth # => “I’m a class method”
p MyClass.new.instance_meth # => “I’m an instance method”
![Page 54: Introduction to the Ruby Object Model](https://reader035.vdocuments.us/reader035/viewer/2022062600/58edf9c01a28ab5c5c8b4623/html5/thumbnails/54.jpg)
superclass
MyClass (RClass)methods table
#instance_meth
MyFunctionality (RModule)
Object (RClass)
(IClass)
methods table
superclass
klass
*(IClass)
methods table
superclass
methods table
#class_meth
MyFunctionality::ClassMethods
(RModule)