[LRUG] Concat and Strings

Andrew Stewart boss at airbladesoftware.com
Tue May 13 07:45:57 PDT 2008


On 13 May 2008, at 15:13, Damian Janowski wrote:

> I don't see any reason for the behaviour you're describing, and a
> quick irb session shows that your syntax is good...
>
> Maybe post the full code?

That was it really.  Anyway, here it is:

   # app/helpers/stylesheets_helper.rb
   module StylesheetsHelper
     def ie6(&block)
       wrap '* html', &block
     end

     private

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

   # app/views/stylesheets/test.ncss
   <% ie6 do %>
     body {
       padding: 4px;
     }
   <% end %>

This is running with my css_dryer plugin.

http://opensource.airbladesoftware.com/trunk/plugins/css_dryer/README


> BTW, you may want to avoid calling capture as much as possible:
>
> def wrap(with, &block)
>   concat("#{with} {", block.binding)
>   yield
>   concat("}", block.binding)
> end

Yes, I agree it's unnecessary here.  I separated out the capture so I  
could print its result for debugging -- I'll ditch it when I figure  
out what's going on.  By the way, did you mean there's something  
actually wrong with capture?

Interestingly, I plugged in your version and discovered I don't need  
the closing brace:

   def wrap(with, &block)
     concat "#{with} {", block.binding
     yield
   end

This gives what we want:

   * html body {
     padding: 4px;
   }

Without the { at the end of the string, I get:

   * html
   body {
     padding: 4px;
   }

So the { seems to swallow the new line.  I can't find this documented  
anywhere....

Thanks and regards,
Andy Stewart

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







More information about the Chat mailing list