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

Newbie question

4 views
Skip to first unread message

len

unread,
Aug 16, 2005, 11:48:26 PM8/16/05
to
Is there some difference in the code I'm not seeing or is one better
than the other:

languages = ['English', 'German', 'Ruby']

languages.each do |lang|
puts 'I love ' + lang + '!'
puts 'Don\'t you?'
end

languages.each { |lang|
puts 'I love ' + lang + '!'
puts "Don't you?"}

Len Sumnler

Ivan Vodopiviz

unread,
Aug 16, 2005, 11:56:32 PM8/16/05
to
AFAIK, both of them do the same thing. you're just changing the block
delimiters.

2005/8/17, len <lsum...@gmail.com>:


--
BlueSteel | | Merkoth


Lothar Scholz

unread,
Aug 17, 2005, 12:02:16 AM8/17/05
to
Hello len,

l> Is there some difference in the code I'm not seeing or is one better
l> than the other:

l> languages = ['English', 'German', 'Ruby']

l> languages.each do |lang|
l> puts 'I love ' + lang + '!'
l> puts 'Don\'t you?'
l> end

l> languages.each { |lang|
l> puts 'I love ' + lang + '!'
l> puts "Don't you?"}


No both are the same, the one liner

languages.each { |lang| puts "I love #{lang}!\nDon't you?" }

is also doing the same and a little bit more rubyish

--
Best regards, emailto: scholz at scriptolutions dot com
Lothar Scholz http://www.ruby-ide.com
CTO Scriptolutions Ruby, PHP, Python IDE 's

Jamal Hansen

unread,
Aug 17, 2005, 12:08:33 AM8/17/05
to
On 8/16/05, len <lsum...@gmail.com> wrote:
>
> Is there some difference in the code I'm not seeing or is one better
> than the other:
>

I think it is a matter of preference / readability though they do the same
thing. If your block is longer than one line the do/end version is preferred
for readability. You can find this in the Poignant Guide <
http://poignantguide.net/ruby> (which I recommend) in Chapter 3 under
Blocks.

Happy coding :)

len

unread,
Aug 17, 2005, 12:37:14 AM8/17/05
to
Thanks everyone

I thought this was the case but I wanted to make sure I didn't miss
something. I will check out the poignant guide.

Len Sumnler

Brian Schröder

unread,
Aug 17, 2005, 5:47:51 AM8/17/05
to

There are two schools regarding the use of {} vs do - end. Some use it
for one-liners against multiple liners, others use to distinguish
blocks where the return value is used vs. blocks where the side effect
is used.
I personally prefer the second option, because it makes code a bit
more salient and easier to read and you don't have to change the
delimiters when reformating code.

E.g.

Use the side-effect:
10.times do puts "Side-Effekt" end

result = []
%w(a b c d).each do | a |
%w(A B C D).each do | b |
result << [a, b]
end
end

Use the return value:
puts Array.new(10) { "Side-Effekt" }

result =
%w(a b c d).inject([]) { | r, a |
%w(A B C D).inject(r) { | r, b |
r << [a, b]
}
}

regards,

Brian

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/


Randy Kramer

unread,
Aug 17, 2005, 4:11:15 PM8/17/05
to

I'm a newbie, but I'm quite certain I've read that there is (was?) a
difference between the two forms of block ( { } vs. ... end) with respect to
scope of variables. Not sure where I read that, but I've been doing reading
online in many places (except the poignant guide) and dead trees Teach
Yourself Ruby in 21 Days and the Pickaxe2.

It doesn't sound like a difference that will affect everyone all the time, but
more one of those things that will bite you occasionally (sp?).

regards,
Randy Kramer


James Edward Gray II

unread,
Aug 17, 2005, 4:22:28 PM8/17/05
to
On Aug 17, 2005, at 3:11 PM, Randy Kramer wrote:

> I'm a newbie, but I'm quite certain I've read that there is (was?) a
> difference between the two forms of block ( { } vs. ... end) with
> respect to
> scope of variables. Not sure where I read that, but I've been
> doing reading
> online in many places (except the poignant guide) and dead trees Teach
> Yourself Ruby in 21 Days and the Pickaxe2.

The difference is in binding. {...} binds tighter than do...end, so:

irb(main):001:0> puts %w{cat bat rat}.map { |w| w.capitalize }
Cat
Bat
Rat
=> nil
irb(main):002:0> puts %w{cat bat rat}.map do |w|
irb(main):003:1* w.capitalize
irb(main):004:1> end
cat
bat
rat
=> nil

Note how the block is associated with different methods here. Add
parenthesis as needed.

James Edward Gray II


David Douthitt

unread,
Aug 17, 2005, 5:44:15 PM8/17/05
to

James Edward Gray II wrote:
> The difference is in binding. {...} binds tighter than do...end

I ALWAYS use {} for block delimiters.

Serious question (which demands a serious answer): why would anyone
want to use do ... end for blocks, anyway?

I'm sure there is a good reason to use them, I just don't know what it
is.

Austin Ziegler

unread,
Aug 17, 2005, 6:45:15 PM8/17/05
to

Binding precedence. do/end has a lower binding than {}, which is part
of what makes Rake possible.

-austin
--
Austin Ziegler * halos...@gmail.com
* Alternate: aus...@halostatue.ca


Brian Schröder

unread,
Aug 18, 2005, 5:50:38 AM8/18/05
to

1) Binding when it is neccessary. (Though I prefer brackets except for
special cases as rake).
2) To differntiate between blocks where the return value is used vs.
blocks where the sideeffekt is used. That makes for faster
code-reading.

Jeff Wood

unread,
Aug 18, 2005, 9:25:12 AM8/18/05
to
Lothar Scholz wrote:

>Hello len,
>
>l> Is there some difference in the code I'm not seeing or is one better
>l> than the other:
>
>l> languages = ['English', 'German', 'Ruby']
>
>l> languages.each do |lang|
>l> puts 'I love ' + lang + '!'
>l> puts 'Don\'t you?'
>l> end
>
>l> languages.each { |lang|
>l> puts 'I love ' + lang + '!'
>l> puts "Don't you?"}
>
>
>No both are the same, the one liner
>
>languages.each { |lang| puts "I love #{lang}!\nDon't you?" }
>
>is also doing the same and a little bit more rubyish
>
>
>

Both are acceptable, but it is common convention that you use {} for
one-liners and use do end for multi-line blocks.

j.

Jeff Wood

unread,
Aug 18, 2005, 9:30:09 AM8/18/05
to
Jeff Wood wrote:

Oh, and there is quite a difference in the evaluation of both within the
interpreter due to order of operations issues. There's a good write up
of this in the PickAxe (The standard Ruby reference material ( book )
Programming Ruby by Dave Thomas)

j.

Randy Kramer

unread,
Aug 18, 2005, 10:54:14 PM8/18/05
to
On Wednesday 17 August 2005 04:22 pm, James Edward Gray II wrote:
> The difference is in binding. {...} binds tighter than do...end, so:

Thanks! (I was sure I had read about some difference.) Thanks also for the
example! (Which I'll have to think about a little ;-)

I did put your examples (and some variants) in IRB and got the same results
you did, but I don't really understand why.

Without the puts, both produce the same result:

irb(main):029:0> %w{cat bat rat}.map do |w| w.capitalize end
=> ["Cat", "Bat", "Rat"]
irb(main):030:0> %w{cat bat rat}.map {|w| w.capitalize}
=> ["Cat", "Bat", "Rat"]

I'm not clear on what binding tighter means, or to what--any further hints
appreciated.

Randy Kramer

Hal Fulton

unread,
Aug 18, 2005, 11:13:45 PM8/18/05
to
Randy Kramer wrote:
>
> I'm not clear on what binding tighter means, or to what--any further hints
> appreciated.

What he means is, it's a matter of precedence.

Observe the examples:

puts %w{cat bat rat}.map { |w| w.capitalize }

puts %w{cat bat rat}.map do |w|
w.capitalize
end

They mean essentially the same as:

puts(%w{cat bat rat}.map { |w| w.capitalize })

puts(%w{cat bat rat}.map) do |w|
w.capitalize
end

In the second one, the map doesn't have a block associated with it --
the block is associated with the puts instead. The map with the
empty block effectively does nothing, and the puts never calls the
block given to it.


Help any?


Hal


Jamal Hansen

unread,
Aug 18, 2005, 11:27:38 PM8/18/05
to
> In the second one, the map doesn't have a block associated with it --
> the block is associated with the puts instead. The map with the
> empty block effectively does nothing, and the puts never calls the
> block given to it.
>
>
So then it would make sense (for clarity) to use the do .. end when
your intent is to act upon an object in the block. When you are
interested in the output of the method with lowest (?) order of
precedence, you would have to use the {..} block form. I think I am
saying this correctly. Thanks for the info.

-Jamal


Randy Kramer

unread,
Aug 19, 2005, 1:28:48 PM8/19/05
to
On Thursday 18 August 2005 11:13 pm, Hal Fulton wrote:
> What he means is, it's a matter of precedence.

--<good stuff snipped>--

> Help any?

Yes, wonderful--the parentheses and thinking of it as precedence made it sink
in. Thanks!

Randy Kramer


Chris Pine

unread,
Aug 23, 2005, 11:50:22 AM8/23/05
to
In general, I tried to show one and only one way to do something.
(This is for beginners, after all... why show two ways to do one thing
when I can show you how to do two different things in the same amount
of time! :)

After you get through the tutorial, I highly recommend picking up a Pickaxe:
http://www.pragmaticprogrammer.com/titles/ruby/

You'll see that there are something like six different ways to make a
string, for example... I've really tried to show just the beginnings
of Ruby. Just enough. It's not at all comprehensive (which the
pickaxe totally is... I didn't see a need for another comprehensive
introduction, just an easier one).

Happy Rubying!

Chris


0 new messages