SlideShare a Scribd company logo
1 of 56
Download to read offline
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Lecture 7 :

Reflection in Ruby
Partly inspired by :
• the pickaxe book “Programming Ruby” by Dave Thomas
• slides by Prof. Tom Mens (UMons)
• slides by Prof. Wolfgang De Meuter (VUB)
LSINF 2335
Programming Paradigms
Prof. Kim Mens
UCL / EPL / INGI
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Advanced language features
■ Higher-order programs
■ Singleton methods
■ Mixin modules
■ Reflective features
■ Metaprogramming with methods
2
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
About blocks, procs and lambdas
■ Different ways of manipulating “chunks of code” in
Ruby:
– methods, blocks, arg& notation, procs, lambda
■ Blocks
– Typical usage:
>> 3.times { print "Ruby" }
– But blocks are not first-class
• Trying these in Ruby produces a syntax error:
>> { print "Ruby" }
>> x = { print "Ruby" }
• Blocks should always be associated to a method invocation, and
yield executes that block in the method
■ But blocks can be turned into first class objects...
3
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
The &arg notation
class ClosureTester
def evaluate_block(arg1,arg2)
yield(arg1,arg2)
end
def reify_block(arg1,arg2,&arg3)
arg3
end
end
c = ClosureTester.new
puts c.evaluate_block(1,2) { |a1,a2| a1 + a2 }
# prints 3
aClosure = c.reify_block(1,2) { |a1,a2| a1 + a2 }
# returns a “procedure” containing the block
puts aClosure.call(3,4)
# prints 7
aClosure.class
# => Proc
4
Blocks are the last
argument of a message.
&arg explicitly refers
to that argument
Note: yield produces an
error when no block is
passed with method
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Proc and lambda
5
■ Procs are a more direct way of turning a block into
a first class closure
aClosure = Proc.new { |a1,a2| a1+a2 }
puts aClosure.call(5,6)
# prints 11
■ Alternative: with proc or lambda:
aClosure = proc { |a1,a2| a1+a2 }
puts aClosure.call(5,6)
# prints 11
aClosure = lambda { |a1,a2| a1+a2 }
puts aClosure.call(5,6)
# prints 11
Note: proc and lambda
are equivalent (but proc
is mildly deprecated)
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Advanced language features
■ Higher-order programs
■ Singleton methods
– singleton classes
– class methods
■ Mixin modules
■ Reflective features
■ Metaprogramming with methods
6
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Singleton methods
■ are methods specific to a particular instance
■ is a form of dynamic object extension
7
class Student
def reflection
puts "I do not understand reflection"
end
end
aStudent = Student.new
aStudent.reflection
# prints : I do not understand
reflection
no equivalent
in Smalltalk
(at language level)
smartStudent = Student.new
def smartStudent.reflection
puts "I understand reflection"
end
smartStudent.reflection
# prints : I understand reflection
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Singleton classes
■ If methods are stored in classes, then where are
singleton methods stored?
■ Ruby creates an anonymous singleton class specific
for the instance
■ This singleton class has the original class of the
instance as superclass
8
instance variables
aStudent
methods for all students
Student
instance variables
smartStudent
singleton methods for smartStudent
anonymous class superclass
class
class
The anonymous singleton class is “hidden”:
smartStudent.class returns Student
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Class methods are singleton methods
9
class Student
def self.some_class_method
puts "I'm the Student class"
end
end
aStudent = Student.new
aStudent.some_class_method
# NoMethodError
Student.some_class_method
# prints : I'm the Student class
methods for all classes
Class
methods for all students
Student
singleton class containing
class methods and
variables for Student class
anonymous class superclass
class
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Some remarks
■ Notice the analogy with Smalltalk
– In Smalltalk, class methods (and variables) are stored in
special meta-classes (one meta-class per class)
– In Ruby, class methods (and variables) are stored in
singleton classes, specific for each class
■ Anonymous singleton classes are “hidden”
– smartStudent.class returns Student
– but there are ways of accessing the singleton class...
10
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Advanced language features
■ Higher-order programs
■ Singleton methods
■ Mixin modules
– alternative to multiple inheritance
■ Reflective features
■ Metaprogramming with methods
11
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Mixin modules
■ What if I want to inherit from multiple classes?
■ Mixin modules are an interesting alternative to
multiple inheritance
– Rather than inheriting from multiple parent classes, you
can “mix in” different modules
■ Mixins modules
– are like a partial class definition
– with late binding
• use of “self” in a mixin method is late bound
12
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Defining a mixin module
13
module Reflection
def reflection
puts "I understand reflection"
end
end
module Debug
def who_am_i
print self.class.name + "(#" +
self.object_id.to_s + ")" +
self.to_s
end
end
a simple mixin module
another mixin module
Late binding of self in class
where module will be mixed in
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Including a mixin in a class
class Student
end
class ReflectionStudent < Student
include Reflection
include Debug
end
aStudent = ReflectionStudent.new
aStudent.reflection
# prints : “I understand reflection”
aStudent.who_am_i
# prints :
ReflectionStudent(#292320)#<ReflectionStudent:0x8ebc0>
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Implementing iterators with the
Enumerable mixin
1512
Kim:~ kimmens$ ri Enumerable
---------------------------------------------------------------------
Class: Enumerable
The +Enumerable+ mixin provides collection classes with several
traversal and searching methods, and with the ability to sort. The
class must provide a method +each+, which yields successive members
of the collection. If +Enumerable#max+, +#min+, or +#sort+ is used,
the objects in the collection must also implement a meaningful
+<=>+ operator, as these methods rely on an ordering between
members of the collection.
---------------------------------------------------------------------
Instance methods:
-----------------
all?, any?, collect, detect, each_cons, each_slice,
each_with_index, entries, enum_cons, enum_slice, enum_with_index,
find, find_all, grep, group_by, include?, index_by, inject, map,
max, member?, min, partition, reject, select, sort, sort_by, sum,
to_a, to_set, zip
...
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Remember this one?
16
■ How can we make such a Collector class in Ruby?
– which implements typical operators on collections like
collect, select, sort, ...
■ Easy:
– implement the method each to iterate over the items
– including the Enumerable mixin does the rest
do: aBlock
^items do: aBlock
select: aBlock
^items select: aBlock
inject: aValue into: aBlock
^items inject: aValue into: aBlock
...
Collector
items
do:

select:
inject:into:
...
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
include Debug
def to_s
@items.to_s + "n"
end
Using the Enumerable mixin
17
include Enumerable
def each
@items.each do |item|
yield item
end
end
class Collector
attr_reader :items
def initialize(collection)
@items = collection
end
end
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Let’s try it out...
18
myCollector = Collector.new([1,3,2])
=> #<Collector:0x321dc4 @items=[1, 3, 2]>
myCollector.who_am_i
Collector (#1642210): 132
=> nil
myCollector.each { |el| print " Value: "+el.to_s }
Value: 1 Value: 3 Value: 2
=> [1, 3, 2]
newCollector = Collector.new(myCollector.sort)
=> #<Collector:0x31aa38 @items=[1, 2, 3]>
newCollector.who_am_i
Collector (#1627420): 123
myCollector.max
=> 3
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
How do mixin modules work?
19
Collector
Object
super-
class
methods
anonymous class
super-
class
mixin methods
Enumerable
methods
anonymous class
super-
class
mixin methods
Debug
ReflectionStudent
super-
class
methods
anonymous class
super-
class
Student
superclass
methods
anonymous class
super-
class
mixin methods
Reflection
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Extending an object with a mixin
20
module Reflection
def reflection
puts "I understand reflection"
end
end
aStudent = Student.new
aStudent.extend Reflection
aStudent.reflection
# prints : “I understand reflection”
otherStudent = Student.new
otherStudent.reflection
# NoMethodError
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Advanced language features
■ Higher-order programs
■ Singleton methods
■ Mixin modules
■ Reflective features
– the class Class
– meta-object protocol
– class-level macros
– eval
– Hook methods
■ Meta-programming with methods
21
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Reflective features in Ruby
■ The class Class
■ Meta-object protocol
■ Class-level Macros
■ Eval
■ Hook methods
22
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Before starting...
■ A bit of introspection at the prompt
■ Did you know that irb had tab completion?
■ Type this and press tab twice
42 shows all messages understood by this object
Kernel:: shows all core methods
or just type nothing and press tab twice to see all
■ Or use ri to read ruby documentation
ri Fixnum for documentation of a class
ri Fixnum#meth for documentation on an instance method
ri Fixnum::meth for documentation on a class method
ri -c for documentation on all classes
■ Click here for more on how to use irb or ri 23
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Going meta...
■ Everything is an object; you can ask any object for its
class :
42.class => Fixnum
:foo.class => Symbol
“Ruby”.class => String
true.class => TrueClass
[1,2,3].class => Array
{ :a => 1 }.class => Hash
■ Even a class is an object; its class is a special class called
Class
Fixnum.class => Class
42.class.class => Class
■ Class is an object too; the class of Class is Class itself
Class.class => Class 24
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
The class Class
■ All classes are an instance of the class Class
■ Class implements some interesting introspection methods
– instance_variables, instance_variable_get,
class_variables, ...
– methods, public_methods, private_instance_methods, ...
– superclass, class, ...
– and many more...
■ And some intercession methods too
– instance_variable_set
■ If you forgot what reflective methods are available, use
introspection :-)
– Class.public_methods.sort
25
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Manipulating a class reflectively
class Example
end
ex = Example.new
ex.instance_variables # => []
ex.instance_variable_set(:@x,1) # => 1
ex.instance_variables # => ["@x"]
ex.instance_variable_defined?(:@x) # => true
ex.instance_variable_defined?(:@y) # => false
ex.instance_variable_get(:@x) # => 1 26
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Reflective features in Ruby
■ The class Class
■ Meta-object protocol
■ Class-level Macros
■ Eval
■ Hook methods
27
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Ruby’s MOP
instance variables
associated class
...
some object
methods
superclass
...
some class
class
superclass
Object
Ruby version 1.8.6
I think that in Ruby version 1.9
Object has a superclass
BasicObject
28
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
29
class
Class
class
Module
superclass
superclass
Ruby version 1.8.6
nil
superclass
instance variables
associated class
...
some object
methods
superclass
...
some class
class
superclass
Object
Ruby’s MOP
class
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
30
Class
class
Module
superclass
superclass
nil
superclass
Object
Ruby’s MOP
class
NilClass
superclass
class class
class
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Reflective features in Ruby
■ The class Class
■ Meta-object protocol
■ Class-level Macros
■ Eval
■ Hook methods
31
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Remember these?
class ComplexCartesian
...
attr_reader :x, :y
attr_writer :x, :y
...
end
class ComplexCartesian
...
attr_accessor :x, :y
...
end
32
or, alternatively:
These are examples of class-level macros
■ i.e., class level methods that generate
code behind the scenes
■ you can also define your own ...
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Let’s try and create our own...
■ Let’s try and create our own class-level macro
■ To define accessor methods that not only read/
write a variable, but also log access to the variable
■ Example of intended usage:
>> ex = Example.new
=> #<Example:0x352208>
>> ex.value=6
Assigning 6 to value
=> 6
>> ex.value
Reading value
=> 6 33
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Let’s start simply...
34
class Logger
def self.add_logging
def log(msg)
STDERR.puts Time.now.strftime("%H:%M:%S: ")
+ "#{self} (#{msg})"
end
end
end
class Example < Logger
add_logging
end
ex = Example.new
ex.log("hello")
This is a class method
This instance method will
get installed when executing
the class method
Install logging method for
instances of the Example
class
Instances of Example now
understand the log method
>> ex.log("hello")
10:11:51: #<Example:0x35ad04> (hello)
=> nil
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
A logged getter and setter...
35
class AttrLogger
def self.add_logged_accessors
def set(newval)
puts "Assigning #{newval}"
@val = newval
end
def get
puts "Reading"
@val
end
end
end
class Example < AttrLogger
add_logged_accessors
end
ex = Example.new
=> #<Example:0x3534c8>
>> ex.set 6
Assigning 6
=> 6
>> ex.get
Reading
=> 6
Class method
Instance method to set value
Instance method to get value
with logging
with logging
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
define_method
■ But how can I create a method of which also the
name is parameterized?
– for example, for a given symbol :value I want to
generate
• an instance variable @value
• a logged reader method named value
• a logged writer method named value=(newvalue)
■ Use define_method
define_method(name) do |param1 ... paramn|
body
end
36
Becomes the method name Becomes the method parameters
Becomes the method body
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Putting everything together...
37
class AttrLogger
def self.attr_logger(name)
define_method("#{name}=") do |val|
puts "Assigning #{val.inspect} to #{name}"
instance_variable_set("@#{name}", val)
end
define_method("#{name}") do
puts "Reading #{name}"
instance_variable_get("@#{name}")
end
end
end
class Example < AttrLogger
attr_logger :value
end
ex = Example.new
=> #<Example:0x3534c8>
>> ex.value=6
Assigning 6 to value
=> 6
>> ex.value
Reading value
=> 6
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Class-level macros in RoR
■ Class-level macros are used heavily in Ruby on Rails
class Project < ActiveRecord::Base
belongs_to :portfolio
has_one :project_manager
has_many :milestones
has_and_belongs_to_many :categories
end
38
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Reflective features in Ruby
■ The class Class
■ Meta-object protocol
■ Class-level Macros
■ Eval
■ Hook methods
39
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
eval
■ Methods instance_eval and class_eval
– defined on Object
– allow you to temporarily set “self” to some arbitrary
object
– evaluates the block, passed with eval, in that context
– then resets “self” to its original value
■ class_eval
– sets self as if you were in the body of the class
definition of the receiver
■ instance_eval
– sets self as if you were working inside the singleton class
of the receiver 40
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic. class_eval
(adding an instance method)
41
class TestClass
end
test = TestClass.new
test.testMethod
# NoMethodError
TestClass.class_eval do
def testMethod
puts "I exist"
end
end
# instance method testMethod has now been added
test.testMethod
# prints: I exist
Define a class
do this as if
inside the class
definition
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic. instance_eval
(adding a singleton method)
42
class TestClass
end
test = TestClass.new
test.testMethod
#NoMethodError
test.instance_eval do
def testMethod
puts "I exist"
end
end
# singleton method testMethod has been added to test
test.testMethod
# prints: I exist
test2 = TestClass.new
test2.testMethod
#NoMethodError
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic. instance_eval
(adding a class method)
43
class TestClass
end
TestClass.instance_eval do
def testMethod
puts "I exist"
end
end
# class method testMethod has now been added
test = TestClass.new
test.testMethod
#NoMethodError
TestClass.testMethod
# prints: I exist
Define a class
do this as if
inside the
singleton class
Confusing terminology:
• class_eval can be used to

define instance methods
• instance-eval can be used
to define class methods
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Reflective features in Ruby
■ The class Class
■ Meta-object protocol
■ Class-level Macros
■ Eval
■ Hook methods
44
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Example of a hook method (1):
Missing methods
class Student
def method_missing(methodid)
puts "I do not understand " + methodid.to_s
end
end
aStudent = Student.new
aStudent.reflection
# prints : “I do not understand reflection”
=> nil
45
Ruby’s equivalent of Smalltalk’s
“doesNotUnderstand”
If Ruby couldn’t find a method after having
looked it up in the object’s class hierarchy,
R u b y l o o k s f o r a m e t h o d c a l l e d
method_missing on the original receiver,
starting back at the class of self and then
looking up the superclass chain again.
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Example of a hook method (1):
Missing methods (ctd.)
class MethodCatcher
def method_missing(name, *args, &block)
puts "Name of missing method is #{name.to_s}."
puts "Method arguments are #{args.to_s}."
puts “Method body is #{block.inspect}."
end
end
catch = MethodCatcher.new
catch.someMethod(1,2){puts "something"}
# prints :
Name of missing method is someMethod.
Method arguments are 12.
Method body is #<Proc:0x0033b2ec@(irb):30>.
46
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Hook methods
■ method_missing
– is a hook method called by Ruby when some method is not
found during method lookup
– variants:
• method_missing(methodid)
• method_missing(name, *args, &block)
■ In general, hook methods (also called callbacks)
– are methods you write
– but that get called by the Ruby interpreter at run-time
when some particular event occurs
47
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Other hook methods
■ Method-related hooks
– adding an instance method: method_added
– adding a singleton method: singleton_method_added
– and many more: method_removed, singleton_method_removed,
method_undefined, singleton_method_undefined
■ Class and module-related hooks
– subclassing: inherited
– mixing in a module: extend_object
– and many more: append_features, included, extended,
initialize_copy, const_missing
■ Object marshaling hooks
– marshal_dump, marshal_load
■ Coercion hooks
– coerce, induced_from, to_xxx
48
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Example of a hook method (2):
Adding methods
49
class MyClass
def self.method_added(name)
puts "Adding Method #{name}"
end
def new_method
# blabla
end
end
Adding Method new_method
=> nil
Hook method
Instance method
hook method is
executed when
Ruby adds the
instance method
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Advanced language features
■ Higher-order programs
■ Singleton methods
■ Mixin modules
■ Reflective features
■ Metaprogramming with methods
– dynamic class extension
– calling method dynamically
– Removing methods dynamically
– Method aliasing
50
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Dynamic class extension:
dynamically add methods to a class
51
class Point
def initialize(x,y)
@x,@y = x,y
end
end
origin = Point.new(0,0)
point = Point.new(0,0)
origin == point # => false
class Point
attr_reader :x, :y
def ==(p)
@x==p.x
end
end
origin == point # => true
newpoint = Point.new(1,0)
origin == newpoint # => false
a simple class
extending the class
(classes are “open”)
old instances see the
new methos too
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Dynamic class extension:
even built-in classes are open
52
[1,2,3].myfind { | entry | entry == 2 }
# => NoMethodError: undefined method ‘myfind’ ...
class Array
def myfind
for i in 0...size
value = self[i]
return value if yield(value)
end
return nil
end
end
[1,2,3].myfind { | entry | true } # => 1
[1,2,3].myfind { | entry | entry == 2 } # => 2
[1,2,3].myfind { | entry | entry > 2 } # => 3
Sidenote: actually
the method find
is already
implemented on
collection classes
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Calling methods dynamically
53
class TestClass
def testMethod
puts "I exist"
end
def testMethod2(arg)
puts "I also exist with argument #{arg}"
end
def method_missing(methodid)
puts "#{methodid} does not exist"
end
end
test = TestClass.new
selector = :testMethod
test.send(:testMethod)
value = 2
test.send("#{selector}2".to_s,value)
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic. Removing methods from a class or
module
54
class TestClass
def testMethod
puts "I exist"
end
def method_missing(methodid)
puts "#{methodid} does not exist"
end
end
test = TestClass.new
test.testMethod # prints: I exist
class TestClass
remove_method(:testMethod)
end
test.testMethod
# prints: testMethod does not exist
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Method aliasing
class Class
alias_method :old_new, :new
def new(*args)
result = old_new(*args)
result.timestamp = Time.now
return result
end
end
class Object
def timestamp
return @timestamp
end
def timestamp=(aTime)
@timestamp = aTime
end
end
class Test
end
obj1 = Test.new
sleep 2
obj2 = Test.new
puts obj1.timestamp
# Thu Nov 13 11:22:06 +0100 2008
puts obj2.timestamp
# Thu Nov 13 11:22:08 +0100 2008
SINF2335:Prog.Paradigms:
Theory,Pract.andApplic.
Conclusion
■ This was only an introduction to some of the
powerful metaprogramming and reflective
features offered by Ruby...
■ ... but there exist many more
■ Just try them out for yourself !
56

More Related Content

Similar to Reflection in Ruby

Basics of reflection in java
Basics of reflection in javaBasics of reflection in java
Basics of reflection in javakim.mens
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPsRavi Bhadauria
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingMH Abid
 
Introduction to Smalltalk
Introduction to SmalltalkIntroduction to Smalltalk
Introduction to Smalltalkkim.mens
 
Python_Unit_3.pdf
Python_Unit_3.pdfPython_Unit_3.pdf
Python_Unit_3.pdfalaparthi
 
Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual Abdul Hannan
 
chapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingchapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingWondimuBantihun1
 
Software Reuse and Object-Oriented Programming
Software Reuse and Object-Oriented ProgrammingSoftware Reuse and Object-Oriented Programming
Software Reuse and Object-Oriented Programmingkim.mens
 
The Ring programming language version 1.5.2 book - Part 71 of 181
The Ring programming language version 1.5.2 book - Part 71 of 181The Ring programming language version 1.5.2 book - Part 71 of 181
The Ring programming language version 1.5.2 book - Part 71 of 181Mahmoud Samir Fayed
 
Review of c_sharp2_features_part_ii
Review of c_sharp2_features_part_iiReview of c_sharp2_features_part_ii
Review of c_sharp2_features_part_iiNico Ludwig
 
Scala for Machine Learning
Scala for Machine LearningScala for Machine Learning
Scala for Machine LearningPatrick Nicolas
 
Object oriented analysis_and_design_v2.0
Object oriented analysis_and_design_v2.0Object oriented analysis_and_design_v2.0
Object oriented analysis_and_design_v2.0Ganapathi M
 

Similar to Reflection in Ruby (20)

Basics of reflection in java
Basics of reflection in javaBasics of reflection in java
Basics of reflection in java
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
 
Php oop (1)
Php oop (1)Php oop (1)
Php oop (1)
 
13 ruby modules
13 ruby modules13 ruby modules
13 ruby modules
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Introduction to Smalltalk
Introduction to SmalltalkIntroduction to Smalltalk
Introduction to Smalltalk
 
Introduction to Design Patterns
Introduction to Design PatternsIntroduction to Design Patterns
Introduction to Design Patterns
 
Python_Unit_3.pdf
Python_Unit_3.pdfPython_Unit_3.pdf
Python_Unit_3.pdf
 
Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual
 
Java reflection
Java reflectionJava reflection
Java reflection
 
chapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programmingchapter 5 concepts of object oriented programming
chapter 5 concepts of object oriented programming
 
Software Reuse and Object-Oriented Programming
Software Reuse and Object-Oriented ProgrammingSoftware Reuse and Object-Oriented Programming
Software Reuse and Object-Oriented Programming
 
مقدمة بايثون .pptx
مقدمة بايثون .pptxمقدمة بايثون .pptx
مقدمة بايثون .pptx
 
The Ring programming language version 1.5.2 book - Part 71 of 181
The Ring programming language version 1.5.2 book - Part 71 of 181The Ring programming language version 1.5.2 book - Part 71 of 181
The Ring programming language version 1.5.2 book - Part 71 of 181
 
Design Patterns - GOF
Design Patterns - GOFDesign Patterns - GOF
Design Patterns - GOF
 
Review of c_sharp2_features_part_ii
Review of c_sharp2_features_part_iiReview of c_sharp2_features_part_ii
Review of c_sharp2_features_part_ii
 
Scala for Machine Learning
Scala for Machine LearningScala for Machine Learning
Scala for Machine Learning
 
Patterns In-Javascript
Patterns In-JavascriptPatterns In-Javascript
Patterns In-Javascript
 
Object oriented analysis_and_design_v2.0
Object oriented analysis_and_design_v2.0Object oriented analysis_and_design_v2.0
Object oriented analysis_and_design_v2.0
 

More from kim.mens

Software Maintenance and Evolution
Software Maintenance and EvolutionSoftware Maintenance and Evolution
Software Maintenance and Evolutionkim.mens
 
Context-Oriented Programming
Context-Oriented ProgrammingContext-Oriented Programming
Context-Oriented Programmingkim.mens
 
Bad Code Smells
Bad Code SmellsBad Code Smells
Bad Code Smellskim.mens
 
Object-Oriented Design Heuristics
Object-Oriented Design HeuristicsObject-Oriented Design Heuristics
Object-Oriented Design Heuristicskim.mens
 
Software Patterns
Software PatternsSoftware Patterns
Software Patternskim.mens
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoringkim.mens
 
Domain Modelling
Domain ModellingDomain Modelling
Domain Modellingkim.mens
 
Object-Oriented Application Frameworks
Object-Oriented Application FrameworksObject-Oriented Application Frameworks
Object-Oriented Application Frameworkskim.mens
 
Towards a Context-Oriented Software Implementation Framework
Towards a Context-Oriented Software Implementation FrameworkTowards a Context-Oriented Software Implementation Framework
Towards a Context-Oriented Software Implementation Frameworkkim.mens
 
Towards a Taxonomy of Context-Aware Software Variabilty Approaches
Towards a Taxonomy of Context-Aware Software Variabilty ApproachesTowards a Taxonomy of Context-Aware Software Variabilty Approaches
Towards a Taxonomy of Context-Aware Software Variabilty Approacheskim.mens
 
Breaking the Walls: A Unified Vision on Context-Oriented Software Engineering
Breaking the Walls: A Unified Vision on Context-Oriented Software EngineeringBreaking the Walls: A Unified Vision on Context-Oriented Software Engineering
Breaking the Walls: A Unified Vision on Context-Oriented Software Engineeringkim.mens
 
Context-oriented programming
Context-oriented programmingContext-oriented programming
Context-oriented programmingkim.mens
 
Basics of reflection
Basics of reflectionBasics of reflection
Basics of reflectionkim.mens
 
Advanced Reflection in Java
Advanced Reflection in JavaAdvanced Reflection in Java
Advanced Reflection in Javakim.mens
 
Introduction to Ruby
Introduction to RubyIntroduction to Ruby
Introduction to Rubykim.mens
 
A gentle introduction to reflection
A gentle introduction to reflectionA gentle introduction to reflection
A gentle introduction to reflectionkim.mens
 
Managing the Evolution of Information Systems with Intensional Views and Rela...
Managing the Evolution of Information Systems with Intensional Views and Rela...Managing the Evolution of Information Systems with Intensional Views and Rela...
Managing the Evolution of Information Systems with Intensional Views and Rela...kim.mens
 
Usage contracts (presented at SATToSE 2014 in L'Aquila, Italy)
Usage contracts (presented at SATToSE 2014 in L'Aquila, Italy)Usage contracts (presented at SATToSE 2014 in L'Aquila, Italy)
Usage contracts (presented at SATToSE 2014 in L'Aquila, Italy)kim.mens
 
Usage contracts in a nutshell
Usage contracts in a nutshellUsage contracts in a nutshell
Usage contracts in a nutshellkim.mens
 
INGI2252 Software Measures & Maintenance
INGI2252 Software Measures & MaintenanceINGI2252 Software Measures & Maintenance
INGI2252 Software Measures & Maintenancekim.mens
 

More from kim.mens (20)

Software Maintenance and Evolution
Software Maintenance and EvolutionSoftware Maintenance and Evolution
Software Maintenance and Evolution
 
Context-Oriented Programming
Context-Oriented ProgrammingContext-Oriented Programming
Context-Oriented Programming
 
Bad Code Smells
Bad Code SmellsBad Code Smells
Bad Code Smells
 
Object-Oriented Design Heuristics
Object-Oriented Design HeuristicsObject-Oriented Design Heuristics
Object-Oriented Design Heuristics
 
Software Patterns
Software PatternsSoftware Patterns
Software Patterns
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
 
Domain Modelling
Domain ModellingDomain Modelling
Domain Modelling
 
Object-Oriented Application Frameworks
Object-Oriented Application FrameworksObject-Oriented Application Frameworks
Object-Oriented Application Frameworks
 
Towards a Context-Oriented Software Implementation Framework
Towards a Context-Oriented Software Implementation FrameworkTowards a Context-Oriented Software Implementation Framework
Towards a Context-Oriented Software Implementation Framework
 
Towards a Taxonomy of Context-Aware Software Variabilty Approaches
Towards a Taxonomy of Context-Aware Software Variabilty ApproachesTowards a Taxonomy of Context-Aware Software Variabilty Approaches
Towards a Taxonomy of Context-Aware Software Variabilty Approaches
 
Breaking the Walls: A Unified Vision on Context-Oriented Software Engineering
Breaking the Walls: A Unified Vision on Context-Oriented Software EngineeringBreaking the Walls: A Unified Vision on Context-Oriented Software Engineering
Breaking the Walls: A Unified Vision on Context-Oriented Software Engineering
 
Context-oriented programming
Context-oriented programmingContext-oriented programming
Context-oriented programming
 
Basics of reflection
Basics of reflectionBasics of reflection
Basics of reflection
 
Advanced Reflection in Java
Advanced Reflection in JavaAdvanced Reflection in Java
Advanced Reflection in Java
 
Introduction to Ruby
Introduction to RubyIntroduction to Ruby
Introduction to Ruby
 
A gentle introduction to reflection
A gentle introduction to reflectionA gentle introduction to reflection
A gentle introduction to reflection
 
Managing the Evolution of Information Systems with Intensional Views and Rela...
Managing the Evolution of Information Systems with Intensional Views and Rela...Managing the Evolution of Information Systems with Intensional Views and Rela...
Managing the Evolution of Information Systems with Intensional Views and Rela...
 
Usage contracts (presented at SATToSE 2014 in L'Aquila, Italy)
Usage contracts (presented at SATToSE 2014 in L'Aquila, Italy)Usage contracts (presented at SATToSE 2014 in L'Aquila, Italy)
Usage contracts (presented at SATToSE 2014 in L'Aquila, Italy)
 
Usage contracts in a nutshell
Usage contracts in a nutshellUsage contracts in a nutshell
Usage contracts in a nutshell
 
INGI2252 Software Measures & Maintenance
INGI2252 Software Measures & MaintenanceINGI2252 Software Measures & Maintenance
INGI2252 Software Measures & Maintenance
 

Recently uploaded

Microteaching on terms used in filtration .Pharmaceutical Engineering
Microteaching on terms used in filtration .Pharmaceutical EngineeringMicroteaching on terms used in filtration .Pharmaceutical Engineering
Microteaching on terms used in filtration .Pharmaceutical EngineeringPrajakta Shinde
 
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptxRESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptxFarihaAbdulRasheed
 
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)riyaescorts54
 
OECD bibliometric indicators: Selected highlights, April 2024
OECD bibliometric indicators: Selected highlights, April 2024OECD bibliometric indicators: Selected highlights, April 2024
OECD bibliometric indicators: Selected highlights, April 2024innovationoecd
 
Four Spheres of the Earth Presentation.ppt
Four Spheres of the Earth Presentation.pptFour Spheres of the Earth Presentation.ppt
Four Spheres of the Earth Presentation.pptJoemSTuliba
 
Fertilization: Sperm and the egg—collectively called the gametes—fuse togethe...
Fertilization: Sperm and the egg—collectively called the gametes—fuse togethe...Fertilization: Sperm and the egg—collectively called the gametes—fuse togethe...
Fertilization: Sperm and the egg—collectively called the gametes—fuse togethe...D. B. S. College Kanpur
 
Speech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptxSpeech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptxpriyankatabhane
 
User Guide: Capricorn FLX™ Weather Station
User Guide: Capricorn FLX™ Weather StationUser Guide: Capricorn FLX™ Weather Station
User Guide: Capricorn FLX™ Weather StationColumbia Weather Systems
 
Pests of safflower_Binomics_Identification_Dr.UPR.pdf
Pests of safflower_Binomics_Identification_Dr.UPR.pdfPests of safflower_Binomics_Identification_Dr.UPR.pdf
Pests of safflower_Binomics_Identification_Dr.UPR.pdfPirithiRaju
 
ECG Graph Monitoring with AD8232 ECG Sensor & Arduino.pptx
ECG Graph Monitoring with AD8232 ECG Sensor & Arduino.pptxECG Graph Monitoring with AD8232 ECG Sensor & Arduino.pptx
ECG Graph Monitoring with AD8232 ECG Sensor & Arduino.pptxmaryFF1
 
Topic 9- General Principles of International Law.pptx
Topic 9- General Principles of International Law.pptxTopic 9- General Principles of International Law.pptx
Topic 9- General Principles of International Law.pptxJorenAcuavera1
 
Pests of Bengal gram_Identification_Dr.UPR.pdf
Pests of Bengal gram_Identification_Dr.UPR.pdfPests of Bengal gram_Identification_Dr.UPR.pdf
Pests of Bengal gram_Identification_Dr.UPR.pdfPirithiRaju
 
Call Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCR
Call Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCRCall Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCR
Call Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCRlizamodels9
 
Citronella presentation SlideShare mani upadhyay
Citronella presentation SlideShare mani upadhyayCitronella presentation SlideShare mani upadhyay
Citronella presentation SlideShare mani upadhyayupadhyaymani499
 
The dark energy paradox leads to a new structure of spacetime.pptx
The dark energy paradox leads to a new structure of spacetime.pptxThe dark energy paradox leads to a new structure of spacetime.pptx
The dark energy paradox leads to a new structure of spacetime.pptxEran Akiva Sinbar
 
Base editing, prime editing, Cas13 & RNA editing and organelle base editing
Base editing, prime editing, Cas13 & RNA editing and organelle base editingBase editing, prime editing, Cas13 & RNA editing and organelle base editing
Base editing, prime editing, Cas13 & RNA editing and organelle base editingNetHelix
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentationtahreemzahra82
 
BUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdf
BUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdfBUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdf
BUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdfWildaNurAmalia2
 
Good agricultural practices 3rd year bpharm. herbal drug technology .pptx
Good agricultural practices 3rd year bpharm. herbal drug technology .pptxGood agricultural practices 3rd year bpharm. herbal drug technology .pptx
Good agricultural practices 3rd year bpharm. herbal drug technology .pptxSimeonChristian
 
Pests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdfPests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdfPirithiRaju
 

Recently uploaded (20)

Microteaching on terms used in filtration .Pharmaceutical Engineering
Microteaching on terms used in filtration .Pharmaceutical EngineeringMicroteaching on terms used in filtration .Pharmaceutical Engineering
Microteaching on terms used in filtration .Pharmaceutical Engineering
 
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptxRESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
 
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)
 
OECD bibliometric indicators: Selected highlights, April 2024
OECD bibliometric indicators: Selected highlights, April 2024OECD bibliometric indicators: Selected highlights, April 2024
OECD bibliometric indicators: Selected highlights, April 2024
 
Four Spheres of the Earth Presentation.ppt
Four Spheres of the Earth Presentation.pptFour Spheres of the Earth Presentation.ppt
Four Spheres of the Earth Presentation.ppt
 
Fertilization: Sperm and the egg—collectively called the gametes—fuse togethe...
Fertilization: Sperm and the egg—collectively called the gametes—fuse togethe...Fertilization: Sperm and the egg—collectively called the gametes—fuse togethe...
Fertilization: Sperm and the egg—collectively called the gametes—fuse togethe...
 
Speech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptxSpeech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptx
 
User Guide: Capricorn FLX™ Weather Station
User Guide: Capricorn FLX™ Weather StationUser Guide: Capricorn FLX™ Weather Station
User Guide: Capricorn FLX™ Weather Station
 
Pests of safflower_Binomics_Identification_Dr.UPR.pdf
Pests of safflower_Binomics_Identification_Dr.UPR.pdfPests of safflower_Binomics_Identification_Dr.UPR.pdf
Pests of safflower_Binomics_Identification_Dr.UPR.pdf
 
ECG Graph Monitoring with AD8232 ECG Sensor & Arduino.pptx
ECG Graph Monitoring with AD8232 ECG Sensor & Arduino.pptxECG Graph Monitoring with AD8232 ECG Sensor & Arduino.pptx
ECG Graph Monitoring with AD8232 ECG Sensor & Arduino.pptx
 
Topic 9- General Principles of International Law.pptx
Topic 9- General Principles of International Law.pptxTopic 9- General Principles of International Law.pptx
Topic 9- General Principles of International Law.pptx
 
Pests of Bengal gram_Identification_Dr.UPR.pdf
Pests of Bengal gram_Identification_Dr.UPR.pdfPests of Bengal gram_Identification_Dr.UPR.pdf
Pests of Bengal gram_Identification_Dr.UPR.pdf
 
Call Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCR
Call Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCRCall Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCR
Call Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCR
 
Citronella presentation SlideShare mani upadhyay
Citronella presentation SlideShare mani upadhyayCitronella presentation SlideShare mani upadhyay
Citronella presentation SlideShare mani upadhyay
 
The dark energy paradox leads to a new structure of spacetime.pptx
The dark energy paradox leads to a new structure of spacetime.pptxThe dark energy paradox leads to a new structure of spacetime.pptx
The dark energy paradox leads to a new structure of spacetime.pptx
 
Base editing, prime editing, Cas13 & RNA editing and organelle base editing
Base editing, prime editing, Cas13 & RNA editing and organelle base editingBase editing, prime editing, Cas13 & RNA editing and organelle base editing
Base editing, prime editing, Cas13 & RNA editing and organelle base editing
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentation
 
BUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdf
BUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdfBUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdf
BUMI DAN ANTARIKSA PROJEK IPAS SMK KELAS X.pdf
 
Good agricultural practices 3rd year bpharm. herbal drug technology .pptx
Good agricultural practices 3rd year bpharm. herbal drug technology .pptxGood agricultural practices 3rd year bpharm. herbal drug technology .pptx
Good agricultural practices 3rd year bpharm. herbal drug technology .pptx
 
Pests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdfPests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdf
 

Reflection in Ruby

  • 1. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Lecture 7 :
 Reflection in Ruby Partly inspired by : • the pickaxe book “Programming Ruby” by Dave Thomas • slides by Prof. Tom Mens (UMons) • slides by Prof. Wolfgang De Meuter (VUB) LSINF 2335 Programming Paradigms Prof. Kim Mens UCL / EPL / INGI
  • 2. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Advanced language features ■ Higher-order programs ■ Singleton methods ■ Mixin modules ■ Reflective features ■ Metaprogramming with methods 2
  • 3. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. About blocks, procs and lambdas ■ Different ways of manipulating “chunks of code” in Ruby: – methods, blocks, arg& notation, procs, lambda ■ Blocks – Typical usage: >> 3.times { print "Ruby" } – But blocks are not first-class • Trying these in Ruby produces a syntax error: >> { print "Ruby" } >> x = { print "Ruby" } • Blocks should always be associated to a method invocation, and yield executes that block in the method ■ But blocks can be turned into first class objects... 3
  • 4. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. The &arg notation class ClosureTester def evaluate_block(arg1,arg2) yield(arg1,arg2) end def reify_block(arg1,arg2,&arg3) arg3 end end c = ClosureTester.new puts c.evaluate_block(1,2) { |a1,a2| a1 + a2 } # prints 3 aClosure = c.reify_block(1,2) { |a1,a2| a1 + a2 } # returns a “procedure” containing the block puts aClosure.call(3,4) # prints 7 aClosure.class # => Proc 4 Blocks are the last argument of a message. &arg explicitly refers to that argument Note: yield produces an error when no block is passed with method
  • 5. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Proc and lambda 5 ■ Procs are a more direct way of turning a block into a first class closure aClosure = Proc.new { |a1,a2| a1+a2 } puts aClosure.call(5,6) # prints 11 ■ Alternative: with proc or lambda: aClosure = proc { |a1,a2| a1+a2 } puts aClosure.call(5,6) # prints 11 aClosure = lambda { |a1,a2| a1+a2 } puts aClosure.call(5,6) # prints 11 Note: proc and lambda are equivalent (but proc is mildly deprecated)
  • 6. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Advanced language features ■ Higher-order programs ■ Singleton methods – singleton classes – class methods ■ Mixin modules ■ Reflective features ■ Metaprogramming with methods 6
  • 7. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Singleton methods ■ are methods specific to a particular instance ■ is a form of dynamic object extension 7 class Student def reflection puts "I do not understand reflection" end end aStudent = Student.new aStudent.reflection # prints : I do not understand reflection no equivalent in Smalltalk (at language level) smartStudent = Student.new def smartStudent.reflection puts "I understand reflection" end smartStudent.reflection # prints : I understand reflection
  • 8. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Singleton classes ■ If methods are stored in classes, then where are singleton methods stored? ■ Ruby creates an anonymous singleton class specific for the instance ■ This singleton class has the original class of the instance as superclass 8 instance variables aStudent methods for all students Student instance variables smartStudent singleton methods for smartStudent anonymous class superclass class class The anonymous singleton class is “hidden”: smartStudent.class returns Student
  • 9. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Class methods are singleton methods 9 class Student def self.some_class_method puts "I'm the Student class" end end aStudent = Student.new aStudent.some_class_method # NoMethodError Student.some_class_method # prints : I'm the Student class methods for all classes Class methods for all students Student singleton class containing class methods and variables for Student class anonymous class superclass class
  • 10. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Some remarks ■ Notice the analogy with Smalltalk – In Smalltalk, class methods (and variables) are stored in special meta-classes (one meta-class per class) – In Ruby, class methods (and variables) are stored in singleton classes, specific for each class ■ Anonymous singleton classes are “hidden” – smartStudent.class returns Student – but there are ways of accessing the singleton class... 10
  • 11. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Advanced language features ■ Higher-order programs ■ Singleton methods ■ Mixin modules – alternative to multiple inheritance ■ Reflective features ■ Metaprogramming with methods 11
  • 12. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Mixin modules ■ What if I want to inherit from multiple classes? ■ Mixin modules are an interesting alternative to multiple inheritance – Rather than inheriting from multiple parent classes, you can “mix in” different modules ■ Mixins modules – are like a partial class definition – with late binding • use of “self” in a mixin method is late bound 12
  • 13. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Defining a mixin module 13 module Reflection def reflection puts "I understand reflection" end end module Debug def who_am_i print self.class.name + "(#" + self.object_id.to_s + ")" + self.to_s end end a simple mixin module another mixin module Late binding of self in class where module will be mixed in
  • 14. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Including a mixin in a class class Student end class ReflectionStudent < Student include Reflection include Debug end aStudent = ReflectionStudent.new aStudent.reflection # prints : “I understand reflection” aStudent.who_am_i # prints : ReflectionStudent(#292320)#<ReflectionStudent:0x8ebc0>
  • 15. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Implementing iterators with the Enumerable mixin 1512 Kim:~ kimmens$ ri Enumerable --------------------------------------------------------------------- Class: Enumerable The +Enumerable+ mixin provides collection classes with several traversal and searching methods, and with the ability to sort. The class must provide a method +each+, which yields successive members of the collection. If +Enumerable#max+, +#min+, or +#sort+ is used, the objects in the collection must also implement a meaningful +<=>+ operator, as these methods rely on an ordering between members of the collection. --------------------------------------------------------------------- Instance methods: ----------------- all?, any?, collect, detect, each_cons, each_slice, each_with_index, entries, enum_cons, enum_slice, enum_with_index, find, find_all, grep, group_by, include?, index_by, inject, map, max, member?, min, partition, reject, select, sort, sort_by, sum, to_a, to_set, zip ...
  • 16. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Remember this one? 16 ■ How can we make such a Collector class in Ruby? – which implements typical operators on collections like collect, select, sort, ... ■ Easy: – implement the method each to iterate over the items – including the Enumerable mixin does the rest do: aBlock ^items do: aBlock select: aBlock ^items select: aBlock inject: aValue into: aBlock ^items inject: aValue into: aBlock ... Collector items do:
 select: inject:into: ...
  • 17. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. include Debug def to_s @items.to_s + "n" end Using the Enumerable mixin 17 include Enumerable def each @items.each do |item| yield item end end class Collector attr_reader :items def initialize(collection) @items = collection end end
  • 18. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Let’s try it out... 18 myCollector = Collector.new([1,3,2]) => #<Collector:0x321dc4 @items=[1, 3, 2]> myCollector.who_am_i Collector (#1642210): 132 => nil myCollector.each { |el| print " Value: "+el.to_s } Value: 1 Value: 3 Value: 2 => [1, 3, 2] newCollector = Collector.new(myCollector.sort) => #<Collector:0x31aa38 @items=[1, 2, 3]> newCollector.who_am_i Collector (#1627420): 123 myCollector.max => 3
  • 19. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. How do mixin modules work? 19 Collector Object super- class methods anonymous class super- class mixin methods Enumerable methods anonymous class super- class mixin methods Debug ReflectionStudent super- class methods anonymous class super- class Student superclass methods anonymous class super- class mixin methods Reflection
  • 20. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Extending an object with a mixin 20 module Reflection def reflection puts "I understand reflection" end end aStudent = Student.new aStudent.extend Reflection aStudent.reflection # prints : “I understand reflection” otherStudent = Student.new otherStudent.reflection # NoMethodError
  • 21. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Advanced language features ■ Higher-order programs ■ Singleton methods ■ Mixin modules ■ Reflective features – the class Class – meta-object protocol – class-level macros – eval – Hook methods ■ Meta-programming with methods 21
  • 22. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Reflective features in Ruby ■ The class Class ■ Meta-object protocol ■ Class-level Macros ■ Eval ■ Hook methods 22
  • 23. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Before starting... ■ A bit of introspection at the prompt ■ Did you know that irb had tab completion? ■ Type this and press tab twice 42 shows all messages understood by this object Kernel:: shows all core methods or just type nothing and press tab twice to see all ■ Or use ri to read ruby documentation ri Fixnum for documentation of a class ri Fixnum#meth for documentation on an instance method ri Fixnum::meth for documentation on a class method ri -c for documentation on all classes ■ Click here for more on how to use irb or ri 23
  • 24. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Going meta... ■ Everything is an object; you can ask any object for its class : 42.class => Fixnum :foo.class => Symbol “Ruby”.class => String true.class => TrueClass [1,2,3].class => Array { :a => 1 }.class => Hash ■ Even a class is an object; its class is a special class called Class Fixnum.class => Class 42.class.class => Class ■ Class is an object too; the class of Class is Class itself Class.class => Class 24
  • 25. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. The class Class ■ All classes are an instance of the class Class ■ Class implements some interesting introspection methods – instance_variables, instance_variable_get, class_variables, ... – methods, public_methods, private_instance_methods, ... – superclass, class, ... – and many more... ■ And some intercession methods too – instance_variable_set ■ If you forgot what reflective methods are available, use introspection :-) – Class.public_methods.sort 25
  • 26. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Manipulating a class reflectively class Example end ex = Example.new ex.instance_variables # => [] ex.instance_variable_set(:@x,1) # => 1 ex.instance_variables # => ["@x"] ex.instance_variable_defined?(:@x) # => true ex.instance_variable_defined?(:@y) # => false ex.instance_variable_get(:@x) # => 1 26
  • 27. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Reflective features in Ruby ■ The class Class ■ Meta-object protocol ■ Class-level Macros ■ Eval ■ Hook methods 27
  • 28. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Ruby’s MOP instance variables associated class ... some object methods superclass ... some class class superclass Object Ruby version 1.8.6 I think that in Ruby version 1.9 Object has a superclass BasicObject 28
  • 29. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. 29 class Class class Module superclass superclass Ruby version 1.8.6 nil superclass instance variables associated class ... some object methods superclass ... some class class superclass Object Ruby’s MOP class
  • 31. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Reflective features in Ruby ■ The class Class ■ Meta-object protocol ■ Class-level Macros ■ Eval ■ Hook methods 31
  • 32. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Remember these? class ComplexCartesian ... attr_reader :x, :y attr_writer :x, :y ... end class ComplexCartesian ... attr_accessor :x, :y ... end 32 or, alternatively: These are examples of class-level macros ■ i.e., class level methods that generate code behind the scenes ■ you can also define your own ...
  • 33. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Let’s try and create our own... ■ Let’s try and create our own class-level macro ■ To define accessor methods that not only read/ write a variable, but also log access to the variable ■ Example of intended usage: >> ex = Example.new => #<Example:0x352208> >> ex.value=6 Assigning 6 to value => 6 >> ex.value Reading value => 6 33
  • 34. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Let’s start simply... 34 class Logger def self.add_logging def log(msg) STDERR.puts Time.now.strftime("%H:%M:%S: ") + "#{self} (#{msg})" end end end class Example < Logger add_logging end ex = Example.new ex.log("hello") This is a class method This instance method will get installed when executing the class method Install logging method for instances of the Example class Instances of Example now understand the log method >> ex.log("hello") 10:11:51: #<Example:0x35ad04> (hello) => nil
  • 35. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. A logged getter and setter... 35 class AttrLogger def self.add_logged_accessors def set(newval) puts "Assigning #{newval}" @val = newval end def get puts "Reading" @val end end end class Example < AttrLogger add_logged_accessors end ex = Example.new => #<Example:0x3534c8> >> ex.set 6 Assigning 6 => 6 >> ex.get Reading => 6 Class method Instance method to set value Instance method to get value with logging with logging
  • 36. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. define_method ■ But how can I create a method of which also the name is parameterized? – for example, for a given symbol :value I want to generate • an instance variable @value • a logged reader method named value • a logged writer method named value=(newvalue) ■ Use define_method define_method(name) do |param1 ... paramn| body end 36 Becomes the method name Becomes the method parameters Becomes the method body
  • 37. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Putting everything together... 37 class AttrLogger def self.attr_logger(name) define_method("#{name}=") do |val| puts "Assigning #{val.inspect} to #{name}" instance_variable_set("@#{name}", val) end define_method("#{name}") do puts "Reading #{name}" instance_variable_get("@#{name}") end end end class Example < AttrLogger attr_logger :value end ex = Example.new => #<Example:0x3534c8> >> ex.value=6 Assigning 6 to value => 6 >> ex.value Reading value => 6
  • 38. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Class-level macros in RoR ■ Class-level macros are used heavily in Ruby on Rails class Project < ActiveRecord::Base belongs_to :portfolio has_one :project_manager has_many :milestones has_and_belongs_to_many :categories end 38
  • 39. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Reflective features in Ruby ■ The class Class ■ Meta-object protocol ■ Class-level Macros ■ Eval ■ Hook methods 39
  • 40. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. eval ■ Methods instance_eval and class_eval – defined on Object – allow you to temporarily set “self” to some arbitrary object – evaluates the block, passed with eval, in that context – then resets “self” to its original value ■ class_eval – sets self as if you were in the body of the class definition of the receiver ■ instance_eval – sets self as if you were working inside the singleton class of the receiver 40
  • 41. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. class_eval (adding an instance method) 41 class TestClass end test = TestClass.new test.testMethod # NoMethodError TestClass.class_eval do def testMethod puts "I exist" end end # instance method testMethod has now been added test.testMethod # prints: I exist Define a class do this as if inside the class definition
  • 42. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. instance_eval (adding a singleton method) 42 class TestClass end test = TestClass.new test.testMethod #NoMethodError test.instance_eval do def testMethod puts "I exist" end end # singleton method testMethod has been added to test test.testMethod # prints: I exist test2 = TestClass.new test2.testMethod #NoMethodError
  • 43. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. instance_eval (adding a class method) 43 class TestClass end TestClass.instance_eval do def testMethod puts "I exist" end end # class method testMethod has now been added test = TestClass.new test.testMethod #NoMethodError TestClass.testMethod # prints: I exist Define a class do this as if inside the singleton class Confusing terminology: • class_eval can be used to
 define instance methods • instance-eval can be used to define class methods
  • 44. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Reflective features in Ruby ■ The class Class ■ Meta-object protocol ■ Class-level Macros ■ Eval ■ Hook methods 44
  • 45. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Example of a hook method (1): Missing methods class Student def method_missing(methodid) puts "I do not understand " + methodid.to_s end end aStudent = Student.new aStudent.reflection # prints : “I do not understand reflection” => nil 45 Ruby’s equivalent of Smalltalk’s “doesNotUnderstand” If Ruby couldn’t find a method after having looked it up in the object’s class hierarchy, R u b y l o o k s f o r a m e t h o d c a l l e d method_missing on the original receiver, starting back at the class of self and then looking up the superclass chain again.
  • 46. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Example of a hook method (1): Missing methods (ctd.) class MethodCatcher def method_missing(name, *args, &block) puts "Name of missing method is #{name.to_s}." puts "Method arguments are #{args.to_s}." puts “Method body is #{block.inspect}." end end catch = MethodCatcher.new catch.someMethod(1,2){puts "something"} # prints : Name of missing method is someMethod. Method arguments are 12. Method body is #<Proc:0x0033b2ec@(irb):30>. 46
  • 47. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Hook methods ■ method_missing – is a hook method called by Ruby when some method is not found during method lookup – variants: • method_missing(methodid) • method_missing(name, *args, &block) ■ In general, hook methods (also called callbacks) – are methods you write – but that get called by the Ruby interpreter at run-time when some particular event occurs 47
  • 48. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Other hook methods ■ Method-related hooks – adding an instance method: method_added – adding a singleton method: singleton_method_added – and many more: method_removed, singleton_method_removed, method_undefined, singleton_method_undefined ■ Class and module-related hooks – subclassing: inherited – mixing in a module: extend_object – and many more: append_features, included, extended, initialize_copy, const_missing ■ Object marshaling hooks – marshal_dump, marshal_load ■ Coercion hooks – coerce, induced_from, to_xxx 48
  • 49. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Example of a hook method (2): Adding methods 49 class MyClass def self.method_added(name) puts "Adding Method #{name}" end def new_method # blabla end end Adding Method new_method => nil Hook method Instance method hook method is executed when Ruby adds the instance method
  • 50. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Advanced language features ■ Higher-order programs ■ Singleton methods ■ Mixin modules ■ Reflective features ■ Metaprogramming with methods – dynamic class extension – calling method dynamically – Removing methods dynamically – Method aliasing 50
  • 51. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Dynamic class extension: dynamically add methods to a class 51 class Point def initialize(x,y) @x,@y = x,y end end origin = Point.new(0,0) point = Point.new(0,0) origin == point # => false class Point attr_reader :x, :y def ==(p) @x==p.x end end origin == point # => true newpoint = Point.new(1,0) origin == newpoint # => false a simple class extending the class (classes are “open”) old instances see the new methos too
  • 52. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Dynamic class extension: even built-in classes are open 52 [1,2,3].myfind { | entry | entry == 2 } # => NoMethodError: undefined method ‘myfind’ ... class Array def myfind for i in 0...size value = self[i] return value if yield(value) end return nil end end [1,2,3].myfind { | entry | true } # => 1 [1,2,3].myfind { | entry | entry == 2 } # => 2 [1,2,3].myfind { | entry | entry > 2 } # => 3 Sidenote: actually the method find is already implemented on collection classes
  • 53. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Calling methods dynamically 53 class TestClass def testMethod puts "I exist" end def testMethod2(arg) puts "I also exist with argument #{arg}" end def method_missing(methodid) puts "#{methodid} does not exist" end end test = TestClass.new selector = :testMethod test.send(:testMethod) value = 2 test.send("#{selector}2".to_s,value)
  • 54. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Removing methods from a class or module 54 class TestClass def testMethod puts "I exist" end def method_missing(methodid) puts "#{methodid} does not exist" end end test = TestClass.new test.testMethod # prints: I exist class TestClass remove_method(:testMethod) end test.testMethod # prints: testMethod does not exist
  • 55. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Method aliasing class Class alias_method :old_new, :new def new(*args) result = old_new(*args) result.timestamp = Time.now return result end end class Object def timestamp return @timestamp end def timestamp=(aTime) @timestamp = aTime end end class Test end obj1 = Test.new sleep 2 obj2 = Test.new puts obj1.timestamp # Thu Nov 13 11:22:06 +0100 2008 puts obj2.timestamp # Thu Nov 13 11:22:08 +0100 2008
  • 56. SINF2335:Prog.Paradigms: Theory,Pract.andApplic. Conclusion ■ This was only an introduction to some of the powerful metaprogramming and reflective features offered by Ruby... ■ ... but there exist many more ■ Just try them out for yourself ! 56