''Macro'' operator

D

Damjan Rems

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
 
A

Alex Gutteridge

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
 
P

Peña, Botp

T24gQmVoYWxmIE9mIERhbWphbiBSZW1zOg0KIyBhMT0xMA0KIyByPSA/KCdhJyArICcxJykgICMg
PyBpcyB3aGF0ZXZlciwgciBzaG91bGQgaGF2ZSB2YWx1ZSAxMA0KDQpzb21lIHNpbXBsZSB3YXlz
LA0KDQpBLiB1c2luZyBldmFsDQo+PiBhMT0xMA0KPT4gMTANCj4+IG1hY3JvPSJhMSsxIg0KPT4g
ImExKzEiDQo+PiBldmFsKG1hY3JvKQ0KPT4gMTENCg0KQi4gdXNpbmcgcHJvYyBvciBsYW1iZGEN
Cj4+IG1hY3JvMiA9IHByb2N7YTErMX0NCj0+ICM8UHJvYzoweGI3ZGQ2YmNjQChpcmIpOjc+DQo+
PiBhMT0xMDANCj0+IDEwMA0KPj4gbWFjcm8yLmNhbGwNCj0+IDEwMQ0KDQpDLiB1c2luZyBzdHJp
bmcgaW50ZXJwb2xhdGlvbg0KPj4gcHV0cyAiYTErMT0je2ExKzF9Ig0KYTErMT0xMDENCj0+IG5p
bA0KDQpwbHMgdGVzdCB0aGVtIGZ1cnRoZXI7IGkgdXN1YWxseSBtYWtlIHN0dXBpZCByZXBsaWVz
Lg0KDQpraW5kIHJlZ2FyZHMgLWJvdHANCg==
 
D

Damjan Rems

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
 
M

Matthias Wächter

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
 
R

Robert Dober

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
 
R

Robert Klemme

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
 
P

Peña, Botp

T24gQmVoYWxmIE9mIERhbWphbiBSZW1zDQojIFRoYW5rcyBldmFsIGlzIGdvb2QuIFdoYXQgSSBo
YXZlIGlzIGRhdGFiYXNlIHdpdGggZmllbGRzIA0KIyBuYW1lMSxuYW1lMixuYW1lMyxuYW1lNC4u
IGFuZA0KDQppIHNlbnNlIG5vcm1hbGl6YXRpb24gcHJvYmxlbS4gc29vbmVyIG9yIGxhdGVyIHlv
dSB3aWxsIHJ1biBvdXQgb2YgZmllbGQgbmFtZXMgZHVlIHRvIGRiIGNvbnN0cmFpbnRzIG9uIG1h
eCBmaWVsZCBjb3VudHMuDQoNCmFzc3VtbWluZywgeW91IGhhdmUgdGhlIHRhYmxlIFRBQkxFIHcg
dGhlIGZmIGZpZWxkcywNCg0KVEFCTEU6IGZvbyBuYW1lMSBuYW1lMiBuYW1lMyBuYW1lNCBuYW1l
NSANCiAgICAgICBhYWEgIG9uZSAgIHR3bw0KICAgICAgIGJiYiAgb25lICAgICAgICAgICAgICBm
b3VyDQogICAgICAgLi4uLi4uDQoNCnlvdSBjYW4gYnJlYWsgaXQgZG93biB0byB0d28gdGFibGVz
IFRBQkxFMSBhbmQgVEFCTEUyDQoNClRBQkxFMTogZm9vIG5hbWUNCiAgICAgICAgYWFhICAgMQ0K
ICAgICAgICBhYWEgICAzDQogICAgICAgIGJiYiAgIDENCiAgICAgICAgYmJiICAgNA0KICAgICAg
ICAgLi4uLi4NCg0KVEFCTEUyOiBuYW1lIHZhbHVlDQogICAgICAgICAgMSAgIG9uZQ0KICAgICAg
ICAgIDIgICB0d28NCiAgICAgICAgICAzICAgdGhyZWUNCiAgICAgICAgICA0ICAgZm91cg0KICAg
ICAgICAgLi4uLi4NCg0KDQpvZiBjb3Vyc2UsIGl0J3MgcG9zc2libGUgaSBzZW5zZWQgaXQgd3Jv
bmcgOikNCg0Ka2luZCByZWdhcmRzIC1ib3RwDQoNCg==
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,317
Members
48,003
Latest member
coldDuece

Latest Threads

Top