From: Brian Candler Date: 2009-02-02T06:10:00+09:00 Subject: Re: Could someone please explain the magic in this code John Small wrote: > (1) what does "<<-" do just before 'END_PRODUCT' > (2) how do the lines after .result b get substituted into END_PRODUCT Search for "here document" in http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html It's essentially an inline string literal, from the line following <<-FOO until the line which has FOO. > (3) How does binding work? The documentation is pretty opaque. What is > it doing in this context and why is it required? A binding is the environment in which variables are evaluated. If you pass a binding, it allows the local variables in your environment to be accessed from elsewhere. irb(main):001:0> def foo(b) irb(main):002:1> puts "x is #{eval("x",b)}" irb(main):003:1> end => nil irb(main):004:0> x = 4 => 4 irb(main):005:0> foo(binding) x is 4 => nil Note that normally the local variable x would not be accessible inside foo, which has its own scope. -- Posted via http://www.ruby-forum.com/.