ruby object graph

9

Click here to load reader

Upload: avilay-parekh

Post on 06-Jul-2015

386 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Ruby object graph

Ruby Object Graphs

Avilay Parekh

Page 2: Ruby object graph

Conventions

object Derived

Base

object, Base, and Derived are all Ruby objects.

object’s class is Derived.

Derived is a subclass of Base.

Page 3: Ruby object graph

Calling Scope

c1@flavor = ‘choc’@calories = 100

Cookieflavor=()flavor ()

Class- new()

Snackcalories=()calories()

Module

Object

BasicObject

class Snack

def initialize

@calories = 100

end

end

class Cookie < Snack

def initialize

@flavor = „choc‟

end

end

c1 = Cookie.new

c1.calories looks in this hierarchy.

Cookie.new looks in this hierarchy.

For obj.method Ruby will look in the hierarchy pointed to by obj’s class.

For method Ruby will look in the hierarchy pointed to by self’s class

For @attribute Ruby will look for the attribute in self.

Page 4: Ruby object graph

Singletons

c1 Class

Define methods on a specific object that will be available to only that object and no other object.

Not the same as the singleton creation design pattern!

This is done by “inserting” an anonymous class at the end of the object’s class hierarchy.

Note, the picture below does not show the usual Module, Object, BasicObject, etc.

class Cookie

# same as before

end

c1 = Cookie.new

def c1.eat

puts “chomp..chomp”

end

c2 = Cookie.new

c1.eat # => “chomp..chomp”

c2.eat # => no method found

Anon-eat()

Cookie

Snack

Page 5: Ruby object graph

Class Methods

c1

Class

Known as “static methods” in C#.

Simply a special case of singletons described in the previous slide.

An anonymous class is inserted at the end of Cookie’s class hierarchy.

Anon-sweet?()

Cookie

Snack

class Cookie

# same as before

def Cookie.sweet?

true

end

end

puts Cookie.sweet?

Page 6: Ruby object graph

class Cookie

# same as before

end

c1 = Cookie.new

def c1.eat

puts “chomp..chomp”

end

class << c1

def munch

puts “yummy!”

end

end

c1.eat

c1.munch

Another way to define singletons

Singletons

class Cookie

# same as before

def Cookie.sweet?

true

end

class << self

def savory?

false

end

end

end

puts Cookie.sweet?

puts Cookie.savory?

Page 7: Ruby object graph

extend object with Module

c1 Class

Instance methods of the module are available as instance methods of that specific object.

Like singleton methods, except loosely coupled.

Anon class points to the Module’s method table instead of having the methods itself.

Done by “including” the module in a newly created singleton class.

Anon

Cookie

Snack

module Recipe

def slice

::

end

end

class Cookie < Snack

# same as before

End

c1 = Cookie.new

c1.extend Recipe

c1.slice

Recipe-slice

methods

Page 8: Ruby object graph

extend class with Module

c1

Instance methods of the module are available as class methods of the class.Like class methods, except loosely coupled.

Cookie

Snack

module Recipe

def slice

::

end

end

class Cookie

extend Recipe

end

Cookie.slice

Anon

Recipe-slice

methods

Class

Page 9: Ruby object graph

include Module in class

c1 Class

Instance methods of the module are available as instance methods of the class.Like OO inheritance, except loosely coupled.

Done by “inserting” an anonymous class in the middle of the object’s class hierarchy.

Cookie

Snack

Anon

Recipe-slice

methods

module Recipe

def slice

::

end

end

class Cookie < Snack

include Recipe

# same as before

End

c1 = Cookie.new

c1.slice

c1.flavor