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
2005/8/17, len <lsum...@gmail.com>:
--
BlueSteel | | Merkoth
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
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 :)
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
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/
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
> 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
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.
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
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.
>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.
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
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
--<good stuff snipped>--
> Help any?
Yes, wonderful--the parentheses and thinking of it as precedence made it sink
in. Thanks!
Randy Kramer
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