# Compute value inside define\_method

**URL:** <https://www.ruby-forum.com/t/compute-value-inside-define-method/184442>\
**Category:** Ruby\
**Created:** [March 2, 2010, 5:05pm UTC](https://www.ruby-forum.com/t/compute-value-inside-define-method/184442 "2010-03-02T17:05:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![karlshea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/58956e/32.png) [@karlshea](https://www.ruby-forum.com/u/karlshea)\
**Post date:** [March 2, 2010, 5:05pm UTC](https://www.ruby-forum.com/t/compute-value-inside-define-method/184442/1 "2010-03-02T17:05:54Z")

</div>

I know there is a ‘trick’ for forcing a value to compute, but I can’t  
seem to remember or find it. The following routine needs to computer  
the value of ‘register += 1’ and not insert it literally.

into.class\_eval do  
define\_method “#{dow}\_#{tod}\_time” do  
@registers.get\_value( “#{register += 1}”.to\_i )  
end  
…snip…  
end

You can see my attempt above, but it doesn’t work. Neither does  
‘eval(“register += 1”)’

What’s the trick? or should I approach the problem from a different  
angle?

---

<div class="post-metadata">

**Author:** ![karlshea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/58956e/32.png) [@karlshea](https://www.ruby-forum.com/u/karlshea)\
**Post date:** [March 2, 2010, 5:15pm UTC](https://www.ruby-forum.com/t/compute-value-inside-define-method/184442/2 "2010-03-02T17:15:53Z")

</div>

On Tue, Mar 2, 2010 at 5:00 PM, Karl [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You can see my attempt above, but it doesn’t work. Neither does  
> ‘eval(“register += 1”)’
> 
> What’s the trick? or should I approach the problem from a different  
> angle?

What is register in your code above? Because this should work:

irb(main):005:0\> register = 0  
=\> 0  
irb(main):006:0\> “#{register += 1}”.to\_i  
=\> 1  
irb(main):008:0\> register  
=\> 1  
irb(main):009:0\> “#{register += 1}”.to\_i  
=\> 2

On the other hand, if register is already and int and the get\_value  
method receives an int, why not just:

@registers.get\_value(register += 1)

?

Jesus.

---

<div class="post-metadata">

**Author:** ![karlshea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/58956e/32.png) [@karlshea](https://www.ruby-forum.com/u/karlshea)\
**Post date:** [March 2, 2010, 5:26pm UTC](https://www.ruby-forum.com/t/compute-value-inside-define-method/184442/3 "2010-03-02T17:26:26Z")

</div>

On Mar 2, 9:15 am, Jesús Gabriel y Galán [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > end  
> > =\> 0  
> > @registers.get\_value(register += 1)
> 
> ?
> 
> Jesus.

‘register’ is an int.

The problem is one of scope. When this is wrapped in the define\_method  
it carries as a variable int object when it is included in a class.  
Thus, when executed via MyClass.weekeday\_morning\_time two consecutive  
times, ‘register += 1’ increments. I don’t want it to increment when  
called, I just need it to increment when it is _defining_ the method.

This is part of a dynamic method creation loop, and the register  
values are fixed. The dynamic method creation allows me to create  
several hundred methods accessing consecutive registers without  
writing all that code. So if I’m defining a method ‘weekday\_day\_time’  
it is ‘get\_value’ from register 21. The ‘register += 1’ is just  
incrementing the counter through the creation loop.

Make sense? I know, this is a bit obscure.

---

<div class="post-metadata">

**Author:** ![karlshea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/58956e/32.png) [@karlshea](https://www.ruby-forum.com/u/karlshea)\
**Post date:** [March 2, 2010, 5:38pm UTC](https://www.ruby-forum.com/t/compute-value-inside-define-method/184442/4 "2010-03-02T17:38:08Z")

</div>

On Tue, Mar 2, 2010 at 5:25 PM, Karl [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > ```
> > > end
> > > 
> > > ```
> > 
> > method receives an int, why not just:  
> > it carries as a variable int object when it is included in a class.
> 
> Make sense? I know, this is a bit obscure.

It’s not a problem of scope. The problem is that define\_method  
receives a block that closes over the register variable. The block  
passed to define\_method is not executed when the define\_method is  
executed, but when the method is invoked, as you have observed. All  
defined methods carry the binding to the same register variable and so  
every invocation of one of those methods will increment the same  
variable. One way to avoid that is force the execution of evaluating  
the register variable when defining the method, for example like:

irb(main):012:0\> 3.times do |i|  
irb(main):013:1\* instance\_eval “def test\_#{i}; #{i};end”  
irb(main):014:1\> end  
=\> 3  
irb(main):015:0\> test\_0  
=\> 0  
irb(main):016:0\> test\_1  
=\> 1  
irb(main):018:0\> test\_2  
=\> 2

The \*eval method family can also receive a String, which is obviously  
not a closure, and so my #{i} above is executed when the instance\_eval  
is executed. Maybe someone can come up with a cleaner solution.  
Translated to your problem (untested):

into.class\_eval do  
instance\_eval \<\<END  
def #{dow}\_#{tod}\_time  
@registers.get\_value(#{register += 1})  
end  
END  
…snip…  
end

Hope this helps.

Jesus.

---

<div class="post-metadata">

**Author:** ![karlshea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/58956e/32.png) [@karlshea](https://www.ruby-forum.com/u/karlshea)\
**Post date:** [March 2, 2010, 6:33pm UTC](https://www.ruby-forum.com/t/compute-value-inside-define-method/184442/5 "2010-03-02T18:33:10Z")

</div>

On Mar 2, 9:37 am, Jesús Gabriel y Galán [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > > ```
> > > > @registers.get_value( "#{register += 1}"to_i )
> > > > 
> > > > ```
> > > 
> > > What is register in your code above? Because this should work:  
> > > On the other hand, if register is already and int and the get\_value  
> > > The problem is one of scope. When this is wrapped in the define\_method  
> > > incrementing the counter through the creation loop.  
> > > the register variable when defining the method, for example like:  
> > > =\> 2  
> > > end  
> > > END  
> > > …snip…  
> > > end
> 
> Hope this helps.
> 
> Jesus.

That was it. I couldn’t remember passing a string to class\_eval forces  
the computation of #{} strings.

Thanks.

If there’s a better way, of course I’d love to know.
