How to store ruby code blocks How to store ruby code blocks ruby ruby

How to store ruby code blocks


There are many ways to do this in Ruby, one of which is to use a Proc:

foo = Proc.new do |test|  puts testend3.upto(8) { foo.call("hello world") }

Read more about Procs:

Update, the above method could be rewritten as follows:

# using lower-case **proc** syntax, all on one linefoo = proc { |test| puts test }3.upto(8) { foo.call("hello world") }# using lambda, just switch the method name from proc to lambdabar = lambda { |test| puts test }3.upto(8) { bar.call("hello world") } 

They're basically very similar methods, with subtle differences.

And finally, there are probably more elegant ways to do what I've outlined, be good to hear from anyone with a better way. Hope this helps.