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
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
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
On Behalf Of Damjan R.:
some simple ways,
A. using eval
a1=10
=> 10macro=“a1+1”
=> “a1+1”eval(macro)
=> 11
B. using proc or lambda
macro2 = proc{a1+1}
=> #Proc:[email protected]:7(irb)a1=100
=> 100macro2.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
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?
On 8/28/07, Damjan R. [email protected] 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
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
On 28.08.2007 13:03, Damjan R. wrote:
Thanks eval is good. What I have is database with fields
name1,name2,name3,name4… andname=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
On Behalf Of Damjan R.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs