# ''Macro'' operator

**URL:** <https://www.ruby-forum.com/t/macro-operator/113172>\
**Category:** Ruby\
**Created:** [August 28, 2007, 11:27am UTC](https://www.ruby-forum.com/t/macro-operator/113172 "2007-08-28T11:27:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [August 28, 2007, 11:27am UTC](https://www.ruby-forum.com/t/macro-operator/113172/1 "2007-08-28T11:27:58Z")

</div>

It is probably called different in ruby, but what I want to achive is:

a1=10  
r= ?(‘a’ + ‘1’) # ? is whatever, r should have value 10

by  
TheR

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [August 28, 2007, 11:37am UTC](https://www.ruby-forum.com/t/macro-operator/113172/2 "2007-08-28T11:37:42Z")

</div>

On 28 Aug 2007, at 18:28, Damjan R. wrote:

> It is probably called different in ruby, but what I want to achive is:
> 
> a1=10  
> r= ?(‘a’ + ‘1’) # ? is whatever, r should have value 10

? is eval:

irb(main):001:0\> a1 = 10  
=\> 10  
irb(main):002:0\> r = eval(‘a’+‘1’)  
=\> 10

There are almost always better ways of doing something than using  
eval though. If you post what you are trying to do someone may be  
able to suggest an alternative.

Alex G.

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [August 28, 2007, 11:42am UTC](https://www.ruby-forum.com/t/macro-operator/113172/3 "2007-08-28T11:42:42Z")

</div>

On Behalf Of Damjan R.:

# a1=10

# r= ?(‘a’ + ‘1’) # ? is whatever, r should have value 10

some simple ways,

A. using eval

> > a1=10  
> > =\> 10
> 
> > macro=“a1+1”  
> > =\> “a1+1”
> 
> > eval(macro)  
> > =\> 11

B. using proc or lambda

> > macro2 = proc{a1+1}  
> > =\> #Proc:0xb7dd6bcc@:7(irb)
> 
> > a1=100  
> > =\> 100
> 
> > macro2.call  
> > =\> 101

C. using string interpolation

> > puts “a1+1=#{a1+1}”  
> > a1+1=101  
> > =\> nil

pls test them further; i usually make stupid replies.

kind regards -botp

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [August 28, 2007, 1:12pm UTC](https://www.ruby-forum.com/t/macro-operator/113172/4 "2007-08-28T13:12:00Z")

</div>

On 28.08.2007 13:03, Damjan R. wrote:

> when 2; name2  
> …etc

Maybe you should change your database scheme to return an array  
name[] with all available name indices?

- Matthias

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [August 28, 2007, 1:50pm UTC](https://www.ruby-forum.com/t/macro-operator/113172/5 "2007-08-28T13:50:03Z")

</div>

On 8/28/07, Damjan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It is probably called different in ruby, but what I want to achive is:
> 
> a1=10  
> r= ?(‘a’ + ‘1’) # ? is whatever, r should have value 10  
> sure ? equals "10 || "

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [August 28, 2007, 1:03pm UTC](https://www.ruby-forum.com/t/macro-operator/113172/6 "2007-08-28T13:03:38Z")

</div>

Thanks eval is good. What I have is database with fields  
name1,name2,name3,name4… and

name=eval(“name#{num}”)

works!

Instead of something like  
name = case num  
when 1; name1  
when 2; name2  
…etc

Thank you  
TheR

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [August 28, 2007, 7:31pm UTC](https://www.ruby-forum.com/t/macro-operator/113172/7 "2007-08-28T19:31:10Z")

</div>

On 28.08.2007 13:03, Damjan R. wrote:

> Thanks eval is good. What I have is database with fields  
> name1,name2,name3,name4… and
> 
> name=eval(“name#{num}”)

I am not sure whether this is intentional but your variable “name” does  
not contain the name but the name’s value.

> works!
> 
> Instead of something like  
> name = case num  
> when 1; name1  
> when 2; name2  
> .etc

Btw, you can also use Ruby’s intelligent string “counting”:

irb(main):001:0\> n=“name1”  
=\> “name1”  
irb(main):002:0\> n.succ  
=\> “name2”  
irb(main):003:0\> n.succ.succ  
=\> “name3”  
irb(main):004:0\> n.succ.succ.succ  
=\> “name4”

But I agree, this seems odd to have. You rather want a DB scheme where  
you store all your values for nameX in another table together with an  
index:

owner\_id (FK to your major table)  
field\_index (numeric)  
value (whatever your type is)

You probably also want a uniqueness constraint on (owner\_id,  
field\_index).

Cheers

robert

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [August 29, 2007, 2:56am UTC](https://www.ruby-forum.com/t/macro-operator/113172/8 "2007-08-29T02:56:09Z")

</div>

On Behalf Of Damjan R.

# Thanks eval is good. What I have is database with fields

# name1,name2,name3,name4… and

i sense normalization problem. sooner or later you will run out of field  
names due to db constraints on max field counts.

assumming, you have the table TABLE w the ff fields,

TABLE: foo name1 name2 name3 name4 name5  
aaa one two  
bbb one four  
…

you can break it down to two tables TABLE1 and TABLE2

TABLE1: foo name  
aaa 1  
aaa 3  
bbb 1  
bbb 4  
…

TABLE2: name value  
1 one  
2 two  
3 three  
4 four  
…

of course, it’s possible i sensed it wrong 🙂

kind regards -botp
