Curly syntax for muliline blocks...

R

Radley Smith

I've seen a mix between the curly syntax and the normal syntax for
multiline blocks. Which do you prefer?
 
R

Robert Klemme

Radley Smith said:
I've seen a mix between the curly syntax and the normal syntax for
multiline blocks. Which do you prefer?

Curly for single line or cases where precedence matters, do-end for
multiline.

I remember a discussion about this some months ago. Maybe you can dig it
up in the archives.

Kind regards

robert
 
A

Anders K. Madsen

I've seen a mix between the curly syntax and the normal syntax for
multiline blocks. Which do you prefer?

This: http://lillesvin.linux.dk/stuff/rubynews.txt ;-)

--
Anders K. Madsen --- http://lillesvin.linux.dk

"There are 10 types of people in the world.
Those who understand binary - and those who don't."


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFBG0PLlNHJe/JASHcRAl+WAJ42uGcNzmVm3QP4ZL4yf64cyWUSGgCfQs9x
spnoYQqfZ4s+484jNqzAyIg=
=i/lD
-----END PGP SIGNATURE-----
 
L

Lloyd Zusman

Radley Smith said:
I've seen a mix between the curly syntax and the normal syntax for
multiline blocks. Which do you prefer?

I always thought that the curly syntax _was_ the normal syntax. :)
 
M

Mark Hubbart

Curly for single line or cases where precedence matters, do-end for
multiline.

I remember a discussion about this some months ago. Maybe you can dig
it
up in the archives.

This is how I do it...

I can't remember ever having read a suggested reason for why the two
block constructs have different precedence. The more I think about it,
the less sense it makes to me. For example:

a = [1,2,3]
==>[1, 2, 3]
b = [[1,1],[2,2],[3,3]]
==>[[1, 1], [2, 2], [3, 3]]
c = a.zip b.map {|x,y| x+y}
==>[[1, 2], [2, 4], [3, 6]]
c = a.zip b.map do |x,y|
x + y
end
TypeError: Array can't be coerced into Fixnum
from (irb):8:in `+'
from (irb):8
from (irb):8:in `zip'
from (irb):8

Using braces, the block goes to b.map. Using do...end, the block is
assigned to a.zip. Fairly straightforward precedence rules.

Now, if you want your braces block to go to zip rather than map, you
can add parenthesis in an appropriate spot:

c = a.zip(b.map) {|x,y| x+y}

But if you want to change the precedence for the do...end type block,
to make it bind to the map method...

c = a.zip( b.map do |x,y|
x + y
end )

well, that's pretty ugly, and pretty confusing. Putting parens around
huge blocks of code like that.

I realize this particular example is rather contrived, but obviously
these things pop up from time to time in one's code. Is there any
particular reason that do...end blocks were given different precedence
from {...} blocks?

cheers,
Mark
 
J

Jim Weirich

Mark Hubbart said:
[... examples elided ...]
I realize this particular example is rather contrived, but obviously
these things pop up from time to time in one's code. Is there any
particular reason that do...end blocks were given different precedence
from {...} blocks?

Given
def g() 1 end
def f(n) yield(n) end

f g do |x| puts "Got #{x}" end # => Got 1
f g { |x| puts "Got #{x}" } # fails

Prints ...

Got 1
x.rb:2:in `f': no block given (LocalJumpError)
from x.rb:5

In Rake, do/end work better with idiomatic rake usage than the curly
version. (See http://rake.rubyforge.org/files/doc/rakefile_rdoc.html at
the bottom of the page).
 

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
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top