VOOZH about

URL: https://rubychallenger.blogspot.com/2011/04/two-ways-to-write-increment-method.html

⇱ The Ruby Challenger: Two ways to write an "increment" method


Sunday, April 3, 2011

Two ways to write an "increment" method

1. As I did in another post:
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

No comments:

Post a Comment

Subscribe to: Post Comments (Atom)