Ruby’s iterators and code blocks

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?

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs