Newbie question

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

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

···

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

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

--
BlueSteel | | Merkoth

Hello 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?"}

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&gt; (which I recommend) in Chapter 3 under
Blocks.

Happy coding :slight_smile:

···

On 8/16/05, len <lsumnler@gmail.com> wrote:

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

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! :slight_smile:

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

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

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

···

On Wednesday 17 August 2005 12:08 am, Jamal Hansen wrote:

On 8/16/05, len <lsumnler@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&gt; (which I recommend) in Chapter 3 under
Blocks.

Lothar Scholz wrote:

Hello 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?"}

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.

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

···

On 17/08/05, len <lsumnler@gmail.com> wrote:

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

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

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

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

···

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.

Jeff Wood wrote:

Lothar Scholz wrote:

Hello 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?"}

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.

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.

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 :wink:

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

···

On Wednesday 17 August 2005 04:22 pm, James Edward Gray II wrote:

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

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

-austin

···

On 8/17/05, David Douthitt <ssrat@mailbag.com> wrote:

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 * halostatue@gmail.com
               * Alternate: austin@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.

regards,

Brian

···

On 17/08/05, David Douthitt <ssrat@mailbag.com> wrote:

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.

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

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

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

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

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

···

On Thursday 18 August 2005 11:13 pm, Hal Fulton wrote: