Newbie question

L

len

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
 
I

Ivan Vodopiviz

AFAIK, both of them do the same thing. you're just changing the block
delimiters.

2005/8/17 said:
Is there some difference in the code I'm not seeing or is one better
than the other:
=20
languages =3D ['English', 'German', 'Ruby']
=20
languages.each do |lang|
puts 'I love ' + lang + '!'
puts 'Don\'t you?'
end
=20
languages.each { |lang|
puts 'I love ' + lang + '!'
puts "Don't you?"}
=20
Len Sumnler
=20
=20
=20


--=20
BlueSteel | | Merkoth
 
L

Lothar Scholz

Hello len,

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

l> languages =3D ['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

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

Jamal Hansen

------=_Part_238_29905333.1124251573095
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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

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

Happy coding :)

------=_Part_238_29905333.1124251573095--
 
L

len

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
 
B

Brian Schröder

Thanks everyone
=20
I thought this was the case but I wanted to make sure I didn't miss
something. I will check out the poignant guide.
=20
Len Sumnler
=20

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 =3D []
%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 =3D
%w(a b c d).inject([]) { | r, a |=20
%w(A B C D).inject(r) { | r, b |
r << [a, b]
}
}

regards,

Brian

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

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

Randy Kramer

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.

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
 
J

James Edward Gray II

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
 
D

David Douthitt

James said:
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.
 
A

Austin Ziegler

I ALWAYS use {} for block delimiters.
=20
Serious question (which demands a serious answer): why would anyone
want to use do ... end for blocks, anyway?
=20
I'm sure there is a good reason to use them, I just don't know what it
is.

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

-austin
--=20
Austin Ziegler * (e-mail address removed)
* Alternate: (e-mail address removed)
 
B

Brian Schröder

=20

=20
I ALWAYS use {} for block delimiters.
=20
Serious question (which demands a serious answer): why would anyone
want to use do ... end for blocks, anyway?
=20
I'm sure there is a good reason to use them, I just don't know what it
is.
=20

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.

regards,

Brian

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

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

Jeff Wood

--------------020105080103080501080805
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Lothar said:
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.

--------------020105080103080501080805--
 
J

Jeff Wood

--------------000306060505030509080507
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Jeff said:
Lothar said:
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.
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.


--------------000306060505030509080507--
 
R

Randy Kramer

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
 
H

Hal Fulton

Randy said:
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
 
J

Jamal Hansen

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.
=20
=20
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
 
C

Chris Pine

In general, I tried to show one and only one way to do something.=20
(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top