Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

what's ||

0 views
Skip to first unread message

duxieweb

unread,
Nov 15, 2009, 7:59:31 AM11/15/09
to
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.

Ralph Shnelvar

unread,
Nov 15, 2009, 9:01:36 AM11/15/09
to
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.

Marnen Laibow-Koser

unread,
Nov 15, 2009, 9:13:30 AM11/15/09
to
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
mar...@marnen.org
--
Posted via http://www.ruby-forum.com/.

7stud --

unread,
Nov 15, 2009, 9:24:27 AM11/15/09
to

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.

7stud --

unread,
Nov 15, 2009, 9:26:27 AM11/15/09
to
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!

Kyle

unread,
Nov 15, 2009, 12:13:35 PM11/15/09
to
[Note: parts of this message were removed to make it a legal post.]

It's the lazy execution boolean or operator

x1 = nil
x2 = [1,2,3]
x3 = [4,5,6]

a = x1 || x2.push(4, 5)
b = x3 || x2.push(6, 7)
# assignment operator version often used to initialize a variable if it
doesn't already have a value
@var ||= 'something'
@var ||= 'whatevs'

after wards:
a == x2 == [1, 2, 3, 4, 5]
b == x3 == [4, 5, 6]
@var == 'something'

Kyle

unread,
Nov 15, 2009, 12:20:18 PM11/15/09
to
[Note: parts of this message were removed to make it a legal post.]

Oh lawd i'm a total jackass for not realizing there was more to the e-mail.
Disregard the previous nonsense. | | is used to indicate a block variable. Ya
gotta read the pickaxe book or another ruby guide for more on what that means:

http://www.ruby-doc.org/docs/ProgrammingRuby/

On Sunday November 15 2009 7:59:31 am duxieweb wrote:

Steve Wilhelm

unread,
Nov 15, 2009, 10:17:33 PM11/15/09
to
Block syntax is also clearly explained in "The Ruby Programming
Language" by Flanagan & Matsumoto in section 5.4.1.

Kyle wrote:
> Oh lawd i'm a total jackass for not realizing there was more to the
> e-mail.
> Disregard the previous nonsense. | | is used to indicate a block
> variable. Ya
> gotta read the pickaxe book or another ruby guide for more on what that
> means:
>
> http://www.ruby-doc.org/docs/ProgrammingRuby/

--
Posted via http://www.ruby-forum.com/.

duxieweb

unread,
Nov 16, 2009, 1:37:29 AM11/16/09
to
2009/11/16 Steve Wilhelm <st...@studio831.com>:

> Block syntax is also clearly explained in "The Ruby Programming
> Language" by Flanagan & Matsumoto in section 5.4.1.
>

Thanks all.
I was reading O'Reilly Learning Ruby, is this a recommended book?

Omran Nazir

unread,
Nov 20, 2009, 9:14:00 AM11/20/09
to
>Thanks all.
>I was reading O'Reilly Learning Ruby, is this a recommended book?>

Yes I highly recommend that you buy and read it from end-to-end. Explanations are clear and succinct. BTW you may also want to read up ion 'yield' as this is what iterators use to return a value to a receiving block of code if one exists. Again, the O'Reilly book will explain all.

Imran Nazir

Imran's profile" border="0">
Friend, Boho, House Owner, Citizen, Engineer

________________________________
From: duxieweb <duxi...@gmail.com>
To: ruby-talk ML <ruby...@ruby-lang.org>
Sent: Mon, 16 November, 2009 6:37:29
Subject: Re: what's ||

0 new messages