Forum: Ruby Ruby’s iterators and code blocks

Posted by Kumar R. (kumar_r)
on 2013-03-09 07:16
From one of the Ruby's resource I found the below statement:

"Ruby’s iterators and code blocks allow you to write and use methods
that are engineered to share their own functionality with their callers.
The method contains some logic and procedure, but when you call the
method,*** you supply additional code that fills out the logic and
individualizes the particular call you’re making***. It’s an elegant
feature with endless applications."

But only the line within the *** I couldn't understand well.

Can anyone help me to reach out to the fact that had been tried to
deliver by that line?
Posted by John W Higgins (Guest)
on 2013-03-09 07:36
(Received via mailing list)
a = [1, 2, 3]

a.each {|x| puts x + 1 } # today I would like to add one during my #each
call
a.each {|x| puts x + 2 } # oops changed my mind and would like to add 
two
now

Both call the #each method with a block that allows me to customized 
what I
do with the exact same items. #each doesn't change but the result of the
call does for each case.

John
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.