Curly vs. Do/End

Ruby discovery for today: curly braces have a different precedence than do/end. That is, these two expressions mean different things:

user.houses.map { |h| h.verified = false if h.hide_avm?; h }.to_xml

user.houses.map do |h| h.verified = false if h.hide_avm?; h end.to_xml

I’m still unsure what the second one means, but I do know that it doesn’t work.

Advertisements

2 Comments

  1. Posted September 6, 2006 at 8:46 pm | Permalink

    Are you sure?

    irb(main):002:0> [3, 2, 1].map { |t| t.next }.sort
    => [2, 3, 4]
    irb(main):003:0> [3, 2, 1].map do |t| t.next; end.sort
    => [2, 3, 4]

  2. Posted September 6, 2006 at 9:04 pm | Permalink

    I couldn’t come up with a simpler example, but the one in my code doesn’t work. There’s also a ‘select’ and arguments to to_xml in my code, too. That might affect it.


%d bloggers like this: