[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

do/end vs braces

Steve Litt

12/8/2005 2:09:00 PM

Hi all,

It looks to me like when you use an iterator (each for instance), you can make
the block either from a do/end pair, or from curly braces. All things being
equal, I'd greatly prefer to use do/end. Are there any differences in runtime
speed, capabilities, or Rubyness?

#!/usr/bin/ruby
(1..4).each do |i|
puts i
end
print "================\n"
(1..4).each { |i|
puts i
}

Output:

[slitt@mydesk slitt]$ ./test.rb
1
2
3
4
================
1
2
3
4
[slitt@mydesk slitt]$

Thanks

SteveT

Steve Litt
http://www.troublesh...
slitt@troubleshooters.com


32 Answers

Joe Van Dyk

12/8/2005 2:14:00 PM

0

On 12/8/05, Steve Litt <slitt@earthlink.net> wrote:
> Hi all,
>
> It looks to me like when you use an iterator (each for instance), you can make
> the block either from a do/end pair, or from curly braces. All things being
> equal, I'd greatly prefer to use do/end. Are there any differences in runtime
> speed, capabilities, or Rubyness?

Typically,

5.times { |i| puts i }

5.times do |i|
puts i
end

Generally people save do .. end for multiline stuff. Don't think
there's a difference in speed.

Sometimes people who use vim use only braces for vim's matching
capabilities, although didn't someone fix that for do..end?


James Gray

12/8/2005 2:47:00 PM

0

On Dec 8, 2005, at 8:13 AM, Joe Van Dyk wrote:

> On 12/8/05, Steve Litt <slitt@earthlink.net> wrote:
>> Hi all,
>>
>> It looks to me like when you use an iterator (each for instance),
>> you can make
>> the block either from a do/end pair, or from curly braces. All
>> things being
>> equal, I'd greatly prefer to use do/end. Are there any differences
>> in runtime
>> speed, capabilities, or Rubyness?
>
> Typically,
>
> 5.times { |i| puts i }
>
> 5.times do |i|
> puts i
> end
>
> Generally people save do .. end for multiline stuff. Don't think
> there's a difference in speed.

Another convention some use is that { ... } are for the times when
you care about the return value and do ... end is for when you're
interested in the side effects. For example:

sum = [1, 2, 3].inject { |sum, n| sum + n } # return value

File.open("total.txt", "w") do |file| # side effects
file.puts sum
end

James Edward Gray II



James Gray

12/8/2005 2:52:00 PM

0

On Dec 8, 2005, at 8:09 AM, Steve Litt wrote:

> Are there any differences in runtime speed, capabilities, or Rubyness?

There is a subtle difference in binding precedence. I wrote about
that in this message:

http://groups.google.com/group/comp.lang.ruby/msg/b67b11...

Hope that helps.

James Edward Gray II



Joe Van Dyk

12/8/2005 2:54:00 PM

0

On 12/8/05, James Edward Gray II <james@grayproductions.net> wrote:
> On Dec 8, 2005, at 8:13 AM, Joe Van Dyk wrote:
>
> > On 12/8/05, Steve Litt <slitt@earthlink.net> wrote:
> >> Hi all,
> >>
> >> It looks to me like when you use an iterator (each for instance),
> >> you can make
> >> the block either from a do/end pair, or from curly braces. All
> >> things being
> >> equal, I'd greatly prefer to use do/end. Are there any differences
> >> in runtime
> >> speed, capabilities, or Rubyness?
> >
> > Typically,
> >
> > 5.times { |i| puts i }
> >
> > 5.times do |i|
> > puts i
> > end
> >
> > Generally people save do .. end for multiline stuff. Don't think
> > there's a difference in speed.
>
> Another convention some use is that { ... } are for the times when
> you care about the return value and do ... end is for when you're
> interested in the side effects. For example:
>
> sum = [1, 2, 3].inject { |sum, n| sum + n } # return value
>
> File.open("total.txt", "w") do |file| # side effects
> file.puts sum
> end

Good point. And also use { ... } if you want to do crazy chains like

[1, 2, 3].collect { |a| i * a }.each { |a| puts a }

Joe


Joe Van Dyk

12/8/2005 2:55:00 PM

0

On 12/8/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
> On 12/8/05, James Edward Gray II <james@grayproductions.net> wrote:
> > On Dec 8, 2005, at 8:13 AM, Joe Van Dyk wrote:
> >
> > > On 12/8/05, Steve Litt <slitt@earthlink.net> wrote:
> > >> Hi all,
> > >>
> > >> It looks to me like when you use an iterator (each for instance),
> > >> you can make
> > >> the block either from a do/end pair, or from curly braces. All
> > >> things being
> > >> equal, I'd greatly prefer to use do/end. Are there any differences
> > >> in runtime
> > >> speed, capabilities, or Rubyness?
> > >
> > > Typically,
> > >
> > > 5.times { |i| puts i }
> > >
> > > 5.times do |i|
> > > puts i
> > > end
> > >
> > > Generally people save do .. end for multiline stuff. Don't think
> > > there's a difference in speed.
> >
> > Another convention some use is that { ... } are for the times when
> > you care about the return value and do ... end is for when you're
> > interested in the side effects. For example:
> >
> > sum = [1, 2, 3].inject { |sum, n| sum + n } # return value
> >
> > File.open("total.txt", "w") do |file| # side effects
> > file.puts sum
> > end
>
> Good point. And also use { ... } if you want to do crazy chains like
>
> [1, 2, 3].collect { |a| i * a }.each { |a| puts a }

Oops, that first 'i' should be an 'a'.


Daniel Schierbeck

12/8/2005 3:07:00 PM

0

Steve Litt wrote:
> Hi all,
>
> It looks to me like when you use an iterator (each for instance), you can make
> the block either from a do/end pair, or from curly braces. All things being
> equal, I'd greatly prefer to use do/end. Are there any differences in runtime
> speed, capabilities, or Rubyness?

There's a slight difference in how they're parsed.

foo bar { block }

is the same as

foo(bar { block })

while

foo bar do
block
end

is the same as

foo(bar) do
block
end

or

foo(bar) { block }

but it's only an issue when you leave out the parantheses. The styling
convention is that the curly braces are used for one-line blocks and the
do..end for multi-line blocks

foo { make_me_a_bar }
bar do
foo = Foo.new
pass_the_foo(foo)
do_something_else
end


Cheers,
Daniel

MenTaLguY

12/8/2005 3:27:00 PM

0

Quoting Joe Van Dyk <joevandyk@gmail.com>:

> 5.times { |i| puts i }
>
> 5.times do |i|
> puts i
> end

> Generally people save do .. end for multiline stuff. Don't think
> there's a difference in speed.

I can promise there's no speed difference. They both parse to the
same thing:

(iter
(call
(lit #<5>) times)
(dasgn_curr i)
(fcall puts
(array
(dvar i))))

(iter
(call
(lit #<5>) times)
(dasgn_curr i)
(fcall puts
(array
(dvar i))))

However, {} and do...end are not totally interchangable as syntax.
These are all equivalent:

obj.meth( 1, 2 ) { |i|
puts i
}

obj.meth( 1, 2 ) do |i|
puts i
end

obj.meth 1, 2 do |i|
puts i
end

But this will net you a parse error ('2' is not a valid method
name):

obj.meth 1, 2 { |i|
puts i
}

And these two parse equivalently:

obj.meth 1, zog { |i|
puts i
}

obj.meth( 1, zog { |i| puts i } )

Think of {} as being more "clingy" than do...end.

Basically, {} has higher precedence than do...end, just as (among
otherwise equivalent operators) && has higher precedence than
'and', and || has higher precedence than 'or'.

I'm sure Ter hates Ruby now. :)

-mental


Martin DeMello

12/8/2005 5:23:00 PM

0

Joe Van Dyk <joevandyk@gmail.com> wrote:
>
> Sometimes people who use vim use only braces for vim's matching
> capabilities, although didn't someone fix that for do..end?

matchit.vim

martin

Steve Litt

12/8/2005 10:37:00 PM

0

On Thursday 08 December 2005 10:07 am, Daniel Schierbeck wrote:
> Steve Litt wrote:
> > Hi all,
> >
> > It looks to me like when you use an iterator (each for instance), you can
> > make the block either from a do/end pair, or from curly braces. All
> > things being equal, I'd greatly prefer to use do/end. Are there any
> > differences in runtime speed, capabilities, or Rubyness?
>
> There's a slight difference in how they're parsed.
>
> foo bar { block }
>
> is the same as
>
> foo(bar { block })
>
> while
>
> foo bar do
> block
> end
>
> is the same as
>
> foo(bar) do
> block
> end
>
> or
>
> foo(bar) { block }
>
> but it's only an issue when you leave out the parantheses.

Except I can't put in the parentheses:

#!/usr/bin/ruby
my_array = ["alpha", "beta", "gamma"]
puts (my_array.collect do
|word|
word.capitalize
end)

Output:
[slitt@mydesk slitt]$ ./test.rb
/test.rb:3: syntax error
/test.rb:6: syntax error
[slitt@mydesk slitt]$

Thanks

SteveT


> The styling
> convention is that the curly braces are used for one-line blocks and the
> do..end for multi-line blocks
>
> foo { make_me_a_bar }
> bar do
> foo = Foo.new
> pass_the_foo(foo)
> do_something_else
> end
>
>
> Cheers,
> Daniel

--
Steve Litt
http://www.troublesh...
slitt@troubleshooters.com


Jacob Fugal

12/8/2005 11:06:00 PM

0

On 12/8/05, Steve Litt <slitt@earthlink.net> wrote:
> Except I can't put in the parentheses:
>
> #!/usr/bin/ruby
> my_array = ["alpha", "beta", "gamma"]
> puts (my_array.collect do
> |word|
> word.capitalize
> end)
>
> Output:
> [slitt@mydesk slitt]$ ./test.rb
> ./test.rb:3: syntax error
> ./test.rb:6: syntax error

Take out the space between puts and the open paren:

~$ cat test.rb
my_array = ["alpha", "beta", "gamma"]
puts(my_array.collect do
|word|
word.capitalize
end)

~$ ruby test.rb
Alpha
Beta
Gamma

Jacob Fugal