On Oct 27, 2003, at 11:43 AM, George Gambill wrote:
> Chris (Gehlker), Thanks for your insight with Ruby. The fact that we
> can
> create a Ruby system, which is normally interruptive, and convert
> (maybe not
> the right word) to "C" thereby allowing us to end up with a compiled
> system,
> makes Ruby very attractive to me.
>
> The opportunities (we don't have problems, only opportunities) with the
> equipment taught us a very valuable lesson. Would it help if (in the
> future) all presentations (and demonstrations) were burned to CD's
> thereby
> providing flexibility the day of the event. Chris, Thanks again.
This is a great idea:
For those still looking for an example of a closure, here is one:
# Closure.rb
# A contrived example of a closure in ruby.
# Since the following definition is not inside
# a class definition, it is defining a new method
# of the base class, Object.
def nTimes(aThing)
return proc { |n| aThing * n }
end
# What just happened there is that the 'proc'
# method converted the the simple block
# ' { |n| aThing * n } into a Proc object.
# Proc objects are closures: they close over
# all the context that applied when the block
# was *defined* including the value of self,
# and any methods, variables and constants
# that apply.
# Let's try it here where aThing is out of
# scope because it was local to the nTimes
# method.
p1 = nTimes(23);
puts p1.call(3); #puts is short for 'put string'
puts p1.call(4); #we only need it if we aren't
#running interactive
p2 = nTimes("Hello ");
puts p2.call(3);
# run like 'ruby Closure.rb
# output looks like
#
# 69
# 92
# Hello Hello Hello