From: John Small Date: 2009-02-02T05:15:55+09:00 Subject: Could someone please explain the magic in this code I need to use erb templates so I went to look at the documentation. It all seems straight forwards until I get to the example where a lot of new coding concepts are pulled in and I'm left totally mystified about what's going on. The code, from the api documentation is
 require "erb"

 # build data class
 class Listings
   PRODUCT = { :name => "Chicken Fried Steak",
               :desc => "A well messages pattie, breaded and fried.",
               :cost => 9.95 }

   attr_reader :product, :price

   def initialize( product = "", price = "" )
     @product = product
     @price = price
   end

   def build
     b = binding
     # create and run templates, filling member data variebles
     ERB.new(<<-'END_PRODUCT'.gsub(/^\s+/, ""), 0, "",
"@product").result b
       <%= PRODUCT[:name] %>
       <%= PRODUCT[:desc] %>
     END_PRODUCT
     ERB.new(<<-'END_PRICE'.gsub(/^\s+/, ""), 0, "", "@price").result b
       <%= PRODUCT[:name] %> -- <%= PRODUCT[:cost] %>
       <%= PRODUCT[:desc] %>
     END_PRICE
   end
 end

 # setup template data
 listings = Listings.new
 listings.build

 puts listings.product + "\n" + listings.price

My questions are;- (1) what does "<<-" do just before 'END_PRODUCT' (2) how do the lines after .result b get substituted into END_PRODUCT and why is there a gsub there. My understanding of the order things are happening is clearly deficient because the gsub to remove white space will have no effect on 'END_PRODUCT' since there's no white space in it. So somehow 'END_PRODUCT' is getting replaced by the lines below, which aren't in quotes, before the gsub happens. wtf is going on here? (3) How does binding work? The documentation is pretty opaque. What is it doing in this context and why is it required? All explanations welcome -- Posted via http://www.ruby-forum.com/.