1.upto(9) { |x| print x }
Why won't this work?
{ print x |x} }
What about y
?
It's for the parameters that are being passed to your block. i.e. in your example, upto
will call your block with each number from 1 to 9 and the current value is available as x
.
The block parameters can have any name, just like method parameters. e.g. 1.upto(9) { |num| puts num }
is valid.
Just like parameters to a method you can also have multiple parameters to a block. e.g.
hash.each_pair { |key, value| puts "#{key} is #{value}" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With