From: Intransition on
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:

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

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:

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

It is kind of like HERE documents, but applied to code. Of course I am
not sure about the syntax either.

Anyway just some out loud thinking.


From: Robert Klemme on
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:
>
> [:a, :b, :c].each do |x|
> define_method("#{x}?") do
> instance_variable_get("@#{x}")
> end
> end
>
> 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:
>
> define_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).

> instance_variable_get("@#{x}")
> end
>
> 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.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
From: Caleb Clausen on
On 5/29/10, Intransition <transfire(a)gmail.com> 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:
>
> [:a, :b, :c].each do |x|
> define_method("#{x}?") do
> instance_variable_get("@#{x}")
> end
> end
>
> 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:
>
> define_method("#{x}?") do <-(x)[:a, :b, :c]
> instance_variable_get("@#{x}")
> end

Maybe you want something like python's array comprehensions? This is
one of the few areas where python really shines in comparison to ruby.

define_method("#{x}?")
instance_variable_get("@#{x}")
end for x in [:a, :b, :c]

The problem I see with this is that x is a local variable, but that
fact won't be apparent to the parser/lexer until it gets to the end of
the expression and sees the for keyword. Not sure how that could be
solved in a way that's at all elegant.

From: Intransition on


On May 30, 5:35 am, Robert Klemme <shortcut...(a)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:
>
> >    [:a, :b, :c].each do |x|
> >      define_method("#{x}?") do
> >        instance_variable_get("@#{x}")
> >      end
> >    end
>
> > 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:
>
> >    define_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).
>
> >      instance_variable_get("@#{x}")
> >    end
>
> > 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

:-)

From: Wes Bailey on

----- "Intransition" <transfire(a)gmail.com> wrote:

> On May 30, 5:35 am, Robert Klemme <shortcut...(a)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:
> >
> > >    [:a, :b, :c].each do |x|
> > >      define_method("#{x}?") do
> > >        instance_variable_get("@#{x}")
> > >      end
> > >    end
> >
> > > 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:
> >
> > >    define_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).
> >
> > >      instance_variable_get("@#{x}")
> > >    end
> >
> > > 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
>
> :-)

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

 |  Next  |  Last
Pages: 1 2
Prev: Another complete beginners question
Next: Square Root