Introduction to the Ruby Object Model

54
Introduction to the Ruby Object Model

Transcript of Introduction to the Ruby Object Model

Page 1: Introduction to the Ruby Object Model

Introduction to the

Ruby Object Model

Page 2: Introduction to the Ruby Object Model

Ruby is an object

oriented language

Page 3: Introduction to the Ruby Object Model

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

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

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

‘a’.upcase # => ‘A’

‘a’.class # => String

Page 7: Introduction to the Ruby Object Model

10.class # => Fixnum

Page 8: Introduction to the Ruby Object Model

class Fixnum

def +(other)

42

end

end

10 + 10 # => 42 (!)

Page 9: Introduction to the Ruby Object Model

Fixnum.class # => Class

Class.class # => Class

Page 10: Introduction to the Ruby Object Model

my_class = Class.new

my_obj = my_class.new

Page 11: Introduction to the Ruby Object Model

The “Ruby Object Model”

Page 12: Introduction to the Ruby Object Model

How Objects Are

Represented Internally

Page 13: Introduction to the Ruby Object Model

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

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

An object has:

• Klass (parent class)

• Flags (frozen? tainted? etc.)

• Instance variables

• Nothing else.

Page 16: Introduction to the Ruby Object Model

How Classes Are

Represented Internally

Page 17: Introduction to the Ruby Object Model

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

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

class Oban < Scotch

AGE = 14

@tasty = true

def tasty

Oban.instance_variable_get("@tasty")

end

end

Page 20: Introduction to the Ruby Object Model

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

Enough with the C

code!

Page 22: Introduction to the Ruby Object Model

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

class User

def name=(name)

@name = name

end

def name

@name

end

end

Page 24: Introduction to the Ruby Object Model

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

class User

def name=(name)

@name = name

end

def name

@name

end

end

me = User.new

Page 26: Introduction to the Ruby Object Model

methods table

#name=

#name

User (RClass)class reference

RObject

instance variables

Page 27: Introduction to the Ruby Object Model

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

class User

def status

:admin

end

end

Page 29: Introduction to the Ruby Object Model

methods table

#name=

#name

#status

User (RClass)class reference

RObject

instance variables

@name => “Peter Cooper”

Page 30: Introduction to the Ruby Object Model

me = User.new

you = User.new

def me.age

30

end

me.age # => ?

you.age # => ?

Page 31: Introduction to the Ruby Object Model

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

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

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

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

me.class # => User

Page 36: Introduction to the Ruby Object Model

“Class Methods”

Page 37: Introduction to the Ruby Object Model

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

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

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

Modules

“Collection of methods

and constants”

Page 41: Introduction to the Ruby Object Model

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

class User

include MyModule

end

me.some_method # => “Hello, world!”

Using modules as “mixins"

Page 43: Introduction to the Ruby Object Model

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

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

Math.cos(10)

Page 46: Introduction to the Ruby Object Model

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

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

methods table

#name=

#name

superclass

User (RClass)

methods table

#name=

#name

#hello

NamingMethods (RModule)

Object (RClass)

Page 49: Introduction to the Ruby Object Model

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

“extend” vs “include”

Page 51: Introduction to the Ruby Object Model

module ClassMethods

def hello

42

end

end

class User

extend ClassMethods

end

p User.hello # => 42

Page 52: Introduction to the Ruby Object Model

module SingletonMethods

def hello

42

end

end

str = “hello”

str.extend SingletonMethods

p str.hello # => 42

Page 53: Introduction to the Ruby Object Model

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

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)