def show a, &b
yield a
end
show 1 {|x| puts x}
but without &b in the definition, the code works too, so I want to know under what circumstance, the &b is required?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A block is normally an anonymous argument to a method. In most cases, you execute the block right there in the method, using yield. There are two cases where yield is not enough:
• You want to pass block to an another method.
• You want to convert the block to a Proc.
In both of these cases you need to refer your block via name. To do that, you attach one more parameter (last arguament always) and use & as prefix to that parameter.
For first case,
For second case, (when you need to convert to Proc)