On Thu, 22 Dec 2005, Austin Ziegler wrote:

> On 20/12/05, Berger, Daniel <Daniel.Berger / qwest.com> wrote:
>> Then ditch heredocs.  I can't think of a case where I desperately needed
>> a heredoc over, say, %q{}.
>
> I use heredocs often. A grammar that doesn't support heredocs won't be
> a Ruby grammar.

agreed.  it's extremely difficult to do any serious metaprogramming without
them.  here is a snippet from something i was working on yesterday:

       def xx_config_attr attr, default = nil

         module_eval <<-code
           module InstanceMethods
             def #{ attr } *argv
               if argv.empty?
                 if defined? @#{ attr }
                   return @#{ attr }
                 else
                   return(self.class.send("#{ attr }"))
                 end
               else
                 return(send("#{ attr }=", argv.shift))
               end
               nil
             end

             def #{ attr }= value
               @#{ attr } = value
             end
           end

           module ClassMethods
             def #{ attr } *argv
               if argv.empty?
                 if defined? @#{ attr }
                   return @#{ attr }
                 else
                   ancestors.each do |a|
                     next if self == a
                     if a.respond_to? "#{ attr }"
                       return(a.send("#{ attr }"))
                     end
                   end
                 end
               else
                 return(send("#{ attr }=", argv.shift))
               end
               nil
             end

             def #{ attr }= value
               @#{ attr } = value
             end
           end

           def self::#{ attr } *argv
             if argv.empty?
               if defined? @#{ attr }
                 return @#{ attr }
               end
             else
               return(send("#{ attr }=", argv.shift))
             end
             nil
           end

           def self::#{ attr }= value
             @#{ attr } = value
           end
         code

         send "#{ attr }=", default
       end

this kind of stuff is extremely difficult to code if you are limited to
symbols like {,(,{, etc to delimit the string.  this is true anytime your
string contains arbitrary code.  heredocs make it possible.

regards.

-a
-- 
===============================================================================
| ara [dot] t [dot] howard [at] noaa [dot] gov
| all happiness comes from the desire for others to be happy.  all misery
| comes from the desire for oneself to be happy.
| -- bodhicaryavatara
===============================================================================