Sunday, April 3, 2011
Two ways to write an "increment" method
1. As I did in another post:
2. Inspired here:
inc = lambda do |x|
eval "#{x} += 1"
end
a = 5
inc[:a]
puts a #=> 6
2. Inspired here:
def inc(&blk)
eval "#{yield} += 1", blk
end
x = 5
inc {:x}
puts x #=> 6
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment