[LRUG] Concat and Strings

Andrew Stewart boss at airbladesoftware.com
Tue May 13 01:23:09 PDT 2008


Hi El Rug,

I can't quite explain the behaviour I'm seeing below.  I'd value any  
explanation.

So...I have a wrap method in a Rails view helper (fragmented for  
debugging):

   def wrap(with, &block)
     inner = capture &block
     s = "#{with} { #{inner} }"
     concat s, block.binding
   end

In the same helper, I have another method which calls the first like  
this:

   def ie6(&block)
     wrap '* html', &block
   end

In my view, a stylesheet template, I do this:

   <% ie6 do %>
     body {
       padding: 4px;
     }
   <% end %>

The rendered template shows what I want, which is:

   * html body {
     padding: 4px;
   }

But I can't explain why it doesn't show this instead:

   * html {
     body {
       padding: 4px;
     }
   }

After all, that is what I get if I insert a "puts s" line in my wrap  
method.

So something about concat is interpreting the { and } in s...but what?

If I remove the { and }, I get this:

   * html
   body {
     padding: 4px;
   }

which makes sense, but isn't what I want.  I want the html and body on  
the same line.

The concat method looks like this:

   # http://github.com/rails/rails/tree/master/actionpack/lib/action_view/helpers/text_helper.rb#L28
   def concat(string, binding)
     eval(ActionView::Base.erb_variable, binding) << string
   end

As far as I can tell, the string is passed through untouched.

Any ideas?

Regards,
Andy Stewart

-------
AirBlade Software
http://airbladesoftware.com







More information about the Chat mailing list