On May 30, 5:35=A0am, Robert Klemme <shortcut... / googlemail.com> wrote:
> On 29.05.2010 21:47, Intransition wrote:
>
>
>
> > I wonder if any other languages have any sort of "multiplicative
> > operation" operator. I was thinking about this today b/c sometimes one
> > has to use a block where it would read better if it could be avoided.
> > For example:
>
> > =A0 =A0[:a, :b, :c].each do |x|
> > =A0 =A0 =A0define_method("#{x}?") do
> > =A0 =A0 =A0 =A0instance_variable_get("@#{x}")
> > =A0 =A0 =A0end
> > =A0 =A0end
>
> > I completely made this up off the top of my head, so ignore what it
> > actually does. The point is rather it would be cool if the loop could
> > described without encasing the code, something like:
>
> > =A0 =A0define_method("#{x}?") do<-(x)[:a, :b, :c]
>
> This can never work because the string is evaluated before the method
> call so you would be defining the same method over and over again (if x
> is actually defined in the surrounding scope).
>
> > =A0 =A0 =A0instance_variable_get("@#{x}")
> > =A0 =A0end
>
> > It is kind of like HERE documents, but applied to code. Of course I am
> > not sure about the syntax either.
>
> I don't see a solution that is better than the explicit loop you
> presented initially.

Yea, I didn't really expect it could be done in Ruby as it now stands.
Though there is this bit of craziness:

  class X
                              lambda { |x|
    define_method("#{x}") do
      instance_variable_get("@#{x}")
    end                       }.call_for_each(:a, :b, :c)

  end

:-)