From: duxieweb on
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.

From: Ralph Shnelvar on
Sunday, November 15, 2009, 5:59:31 AM, you wrote:

d> sorry just was learning ruby so have some low level questions, :)

d> in this statement:

d> 1.upto(10) do |c| print c," " end

d> what's the usage of "| ... |" in ruby? can't easily understand.

d> Thanks.

I'm just learning Ruby, too .. and I found this fairly tough to wrap
my head around. This is what I think I know:

Almost EVERYTHING in Ruby is an object. Thus the "1" in "1.upto(10)" is an
object.

Object have methods that can be applied to them. Or, equivalently,
objects can have messages sent to them. "upto" is method that can be
applied to the integer 1.

"upto" tokes an argument "10".

- - -

Now here's where I get vaguer.

"upto" is a method that, in turn, can call subroutines. One of the
subroutines it can call is ...
do |c| print c," " end

The object.method known as 1.upto(10) will call the subroutine
do |c| print c," " end
ten times.

Each time it calls
do |c| print c," " end
it will pass a parameter to the subroutine. In this case it will pass
1 the first time, 2 the second time ... 10 the last time.

The subroutine
do |c| print c," " end
will see that parameter as c

Thus, the subroutine
do |c| print c," " end
has to know a fair bit about the method "upto" in order to know what
"upto" will pass to the subroutine. That is, it has to know a fair
bit about the public interface of "upto" and what "upto" is supposed
to do. Internal implementation of "upto" is, of course, nearly
irrelevant.

Like in most high level languages, the names of the parameters and the
arguments do not have to be the same. Thus,
do |c| print c," " end
is exactly equivalent to
do |parm| print parm," " end



I hope this helps.



From: Marnen Laibow-Koser on
Ralph Shnelvar wrote:
> Sunday, November 15, 2009, 5:59:31 AM, you wrote:
>
> d> sorry just was learning ruby so have some low level questions, :)
>
> d> in this statement:
>
> d> 1.upto(10) do |c| print c," " end
>
> d> what's the usage of "| ... |" in ruby? can't easily understand.

It declares block arguments. See the sections in the Pickaxe Book on
blocks.

>
> d> Thanks.
>
> I'm just learning Ruby, too .. and I found this fairly tough to wrap
> my head around. This is what I think I know:
>
> Almost EVERYTHING in Ruby is an object. Thus the "1" in "1.upto(10)" is
> an
> object.

Right.

>
> Object have methods that can be applied to them. Or, equivalently,
> objects can have messages sent to them. "upto" is method that can be
> applied to the integer 1.
>
> "upto" tokes an argument "10".

Right.

>
> - - -
>
> Now here's where I get vaguer.
>
> "upto" is a method that, in turn, can call subroutines.

Blocks -- "subroutine" is not a Ruby term.

> One of the
> subroutines it can call is ...
> do |c| print c," " end

It can call any block at all.

>
> The object.method known as 1.upto(10) will call the subroutine
> do |c| print c," " end
> ten times.
>
> Each time it calls
> do |c| print c," " end
> it will pass a parameter to the subroutine. In this case it will pass
> 1 the first time, 2 the second time ... 10 the last time.

Correct.

>
> The subroutine
> do |c| print c," " end
> will see that parameter as c

Also correct.

>
> Thus, the subroutine
> do |c| print c," " end
> has to know a fair bit about the method "upto" in order to know what
> "upto" will pass to the subroutine.

No. The block has to know that upto will yield it one object. That's
all it has to know.

> That is, it has to know a fair
> bit about the public interface of "upto" and what "upto" is supposed
> to do.

Nope. The block expects one object. It doesn't really care whether
it's being yielded to from 1.upto(10), or ['e', 'd', :c, {:b =>
3957}].each, or anything else that will yield it one object (of any
kind) as an argument. In fact, you could store the block in a variable
and yield to it from each of those methods in turn.

> Internal implementation of "upto" is, of course, nearly
> irrelevant.

Totally irrelevant, actually.

>
> Like in most high level languages, the names of the parameters and the
> arguments do not have to be the same. Thus,
> do |c| print c," " end
> is exactly equivalent to
> do |parm| print parm," " end

You're right that those are equivalent, but it has nothing to do with
your statement about parameters and arguments.
>
>
>
> I hope this helps.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen(a)marnen.org
--
Posted via http://www.ruby-forum.com/.

From: 7stud -- on
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/.

From: 7stud -- on
7stud -- wrote:
> duxieweb wrote:
>
> ruby then assigns each value produced by the iterator to the block's
> parameter variable...

which is delimited with the |....| you asked about!
--
Posted via http://www.ruby-forum.com/.