Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

7.
MetaProgramming and reflection
1. Reflection lets us ask an object questions about itself
and have it modify itself.
2. Metaprogramming lets us define new code at runtime.
3. How can these make our code DRYer, more concise, or
easier to read? – (or are they just twenty-dollar words
to make me look smart?)

25.
Turning iterators inside-out
• Java:
– You hand me each element of that collection in turn.
– I’ll do some stuff.
– Then I’ll ask you if there’s any more left.
• Ruby:
– Here is some code to apply to every element of the
collection.
– You manage the iteration or data structure traversal.
And give me result.

26.
Blocks are Closures
• A closure is the set of all variable bindings you can “
see ” at a given point in time
– In Scheme, it’s called an environment
• Blocks are closures: they carry their environment around
with them
• Result: blocks can help reuse by separating what to do
from where & when to do it

28.
Adding methods in the context of an object
1. Is it possible to call private method of an object from
outside the class?
2. Declaring the class methods is the classical example of
adding methods to the objects.

37.
Lambda is just like procs so what’s the difference?
1. unlike Procs, lambdas check the number of arguments
passed.
def args(code)
one, two = 1, 2
code.call(one, two)
end
args(Proc.new{|a, b, c| puts "Give me a #{a} and a #{b} and a #{c.class}"})
args(lambda{|a, b, c| puts "Give me a #{a} and a #{b} and a #{c.class}"})