views:

115

answers:

2

How does rails get away with the following in an .erb file?

<%= yield :sidebar %>
<%= yield :other_bar %>
<%= yield :footer %>

How are they able to yield multiple times in the same context to different symbols? Is this some kind of rails magic?

I'm totally familiar with:

def some_method(arg1, arg2, &block)
 yield(:block)
end

To my knowledge following doesn't work:

def some_incorrect_method(arg1, &block1, &block2)
 yield(:block1)
 yield(:block2)
end

So how are they doing it? How do they make it work?

+4  A: 

They are passing a symbol into yield...

yield :symbol

...not yielding to a different block.

It works more like this:

def some_method(arg1, arg2, &block)
  yield(:some_symbol1)
  yield(:some_symbol2)
end

some_method do |symbol|
  case symbol
  when :some_symbol1
     # do A
  when :some_symbol2
     # do B
  else
     # unrecognised symbol?
  end
end
fd
Thanks. This makes perfect sense!
haroldcampbell