From: 7stud -- Date: 2009-11-15T23:24:27+09:00 Subject: Re: what's || duxieweb wrote: > sorry just was learning ruby so have some low level questions, :) > > in this statement: > > 1.upto(10) do |c| print c," " end > > what's the usage of "| ... |" in ruby? can't easily understand. > > Thanks. The topic you want to read about is "blocks". In ruby, upto() is an iterator, which is something that produces one value at a time. To "catch" those values, you specify what's called a "block", which is like writing a method definition right next to the call to the upto() iterator: 1.upto(10) {|num| puts num} ruby then assigns each value produced by the iterator to the block's parameter variable. The longer multi line syntax looks like this: 1.upto(10) do |num| puts num puts num * 2 end Generally, you use the short syntax when there is only one line to execute inside your "method" definition". If there is more than one line inside the "method", you use the multiline form which requires that you insert 'do' between the call to the iterator and the block. There is one slight difference between the shorter syntax and the longer syntax, but it is unimportant at this point. -- Posted via http://www.ruby-forum.com/.