(1..5).each do|x| puts yield(x) end do |x| return x*2 end
In my head this would loop 1 through 5 call the first block that would yield to the second block and put 2,4,6,8,10
Why does this not work and whats the easiest way to write this.
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.
yieldworks within the methods. Quote from “Programming Ruby”:So, if you want to make this code working, you can change it to something like this:
If you don’t want to define method you should put block into the variable and then use it: