[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: Inject Loop Syntax
From:       Intransition <transfire () gmail ! com>
Date:       2010-05-31 16:30:24
Message-ID: d74e4d71-1cca-4844-8f4c-ef6013469ce6 () v18g2000vbc ! googlegroups ! com
[Download RAW message or body]

On May 30, 6:17 pm, Wes Bailey <w...@verticalresponse.com> wrote:
> irb(main):074:0> class MyTest
> irb(main):075:1> class_eval %w/a b c/.inject( '' ) { |methods,var| methods + "def \
> hello_#{var}?; puts \"hello_#{var}\"; end\n" } irb(main):076:1> end
> => nil
> irb(main):077:0> t = MyTest.new
> => #<MyTest:0x86e98>
> irb(main):078:0> t.hello_a?
> hello_a
> => nil
> irb(main):079:0> t.hello_b?
> hello_b
> => nil
> irb(main):080:0> t.hello_c?
> hello_c
> => nil

Yes, that can be done. However, the point was to get the body of loop
to be the thing that stands-out first rather than the loop. My example
was kind of a joke. I just pushed the code that does the loop off to
the right, and the lambda was just for fun. I could have done the same
with the original

                                       [:a, :b, :c].each{ |x|
      define_method("#{x}?") do
        instance_variable_get("@#{x}")
      end
                                       }


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic