Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Ruby, what are the vertical lines?

Tags:

ruby

1.upto(9) { |x| print x }

Why won't this work?

{ print x |x} }

What about y?

like image 971
TIMEX Avatar asked Aug 23 '10 08:08

TIMEX


1 Answers

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}" }
like image 61
mikej Avatar answered Nov 15 '22 13:11

mikej