I want to store a “code block” in a variable to be reused, something like:
block = do
|test| puts test
end
3.upto(8) block
Can someone show me what am I doing so obviously wrong? (Or if it’s just impossible)
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.
There are many ways to do this in Ruby, one of which is to use a Proc:
Read more about Procs:
Update, the above method could be rewritten as follows:
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.