# Inject Loop Syntax

**URL:** <https://www.ruby-forum.com/t/inject-loop-syntax/189367>\
**Category:** Ruby\
**Created:** [May 29, 2010, 9:48pm UTC](https://www.ruby-forum.com/t/inject-loop-syntax/189367 "2010-05-29T21:48:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [May 29, 2010, 9:48pm UTC](https://www.ruby-forum.com/t/inject-loop-syntax/189367/1 "2010-05-29T21:48:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [May 30, 2010, 11:35am UTC](https://www.ruby-forum.com/t/inject-loop-syntax/189367/2 "2010-05-30T11:35:49Z")

</div>

On 29.05.2010 21:47, Intransition wrote:

> 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

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [May 30, 2010, 1:14pm UTC](https://www.ruby-forum.com/t/inject-loop-syntax/189367/3 "2010-05-30T13:14:44Z")

</div>

On 5/29/10, Intransition [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [May 31, 2010, 12:18am UTC](https://www.ruby-forum.com/t/inject-loop-syntax/189367/4 "2010-05-31T00:18:25Z")

</div>

----- “Intransition” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > For example:  
> > > could  
> > > Â Â Â instance\_variable\_get(“@#{x}”)  
> > > stands.  
> > > 🙂  
> > > 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

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [May 31, 2010, 6:31pm UTC](https://www.ruby-forum.com/t/inject-loop-syntax/189367/5 "2010-05-31T18:31:11Z")

</div>

On May 30, 6:17 pm, Wes B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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
                                   }

```

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [May 30, 2010, 3:32pm UTC](https://www.ruby-forum.com/t/inject-loop-syntax/189367/6 "2010-05-30T15:32:52Z")

</div>

On May 30, 5:35 am, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > define_method("#{x}?") do
> > 
> > ```
> 
> This can never work because the string is evaluated before the method  
> 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

🙂
