[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

''Macro'' operator

Damjan Rems

8/28/2007 9:28:00 AM


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
--
Posted via http://www.ruby-....

7 Answers

Alex Gutteridge

8/28/2007 9:36:00 AM

0

On 28 Aug 2007, at 18:28, Damjan Rems 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 Gutteridge

Bioinformatics Center
Kyoto University



Peña, Botp

8/28/2007 9:41:00 AM

0

On Behalf Of Damjan Rems:
# 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@(irb):7>
>> 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

Damjan Rems

8/28/2007 11:04:00 AM

0


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

--
Posted via http://www.ruby-....

Matthias Wächter

8/28/2007 11:10:00 AM

0

On 28.08.2007 13:03, Damjan Rems wrote:
> 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

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

- Matthias

Robert Dober

8/28/2007 11:49:00 AM

0

On 8/28/07, Damjan Rems <d_rems@yahoo.com> 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
>
> by
> TheR
> --
> Posted via http://www.ruby-....
>
>


--
I'm an atheist and that's it. I believe there's nothing we can know
except that we should be kind to each other and do what we can for
other people.
-- Katharine Hepburn

Robert Klemme

8/28/2007 5:30:00 PM

0

On 28.08.2007 13:03, Damjan Rems 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

Peña, Botp

8/29/2007 12:55:00 AM

0

On Behalf Of Damjan Rems
# 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