instance_variable_[gs]et

T

Trans

Just spinning wheels this evening...

Why does #instance_variable_get/set require an "@" sign in front of
the variable name? It's not like there is any other valid instance
variable name, is there?

And why can't #instance_variable_set take a hash to set more than one
variable at a time?

T.
 
F

Florian Gilcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

And why can't #instance_variable_set take a hash to set more than one
variable at a time?

T.



By the way, if you really need it, it is easily implemented on your own:
Example:

def set_instance_variables(variables)
variables.each do |key, value|
instance_variable_set("@#{key}", value)
end
end

Greetings
Florian
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkfrkdgACgkQJA/zY0IIRZYEZgCfSL2ym/23mZbHJvlKQhBc2UXu
cXsAoJNTIlNFcPDVDAwO14KH4/wtsJ/h
=xzdH
-----END PGP SIGNATURE-----
 
T

Trans

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1




By the way, if you really need it, it is easily implemented on your own:
Example:

def set_instance_variables(variables)
variables.each do |key, value|
instance_variable_set("@#{key}", value)
end
end

Yes, of course. Facets has #instance_assign, but it just seems
wasteful when one method could do.

I don't quite get matz take. How is

variables.each do |key, value|
instance_variable_set("@#{key}", value)
end

cleaner than

instance_variable_set(variables)

T.
 
R

Robert Klemme

Yes, of course. Facets has #instance_assign, but it just seems
wasteful when one method could do.

I don't quite get matz take. How is

variables.each do |key, value|
instance_variable_set("@#{key}", value)
end

cleaner than

instance_variable_set(variables)

How often is that really needed? I can remember only few cases where I
needed that - and those were probably more experimental.

Kind regards

robert
 
J

Joel VanderWerf

Robert said:
On 27.03.2008 16:28, Trans wrote: ...

How often is that really needed? I can remember only few cases where I
needed that - and those were probably more experimental.

I've got a couple of methods in library code I use frequently that
initialize or populate an object from a hash using the loop above. I've
always thought it was kind of a pain (and inefficient) to have to
construct a string for each key.

Why couldn't #instance_variable_set be agnostic about whether you refer
to ivars as "foo" or "@foo" ? I don't see any ambiguity...
 
R

Rados³aw Bu³at

T24gVGh1LCBNYXIgMjcsIDIwMDggYXQgNDoyOCBQTSwgVHJhbnMgPHRyYW5zZmlyZUBnbWFpbC5j
b20+IHdyb3RlOgoKPiAgSSBkb24ndCBxdWl0ZSBnZXQgbWF0eiB0YWtlLiBIb3cgaXMKPgo+Cj4g
ICAgdmFyaWFibGVzLmVhY2ggZG8gfGtleSwgdmFsdWV8Cj4gICAgICBpbnN0YW5jZV92YXJpYWJs
ZV9zZXQoIkAje2tleX0iLCB2YWx1ZSkKPiAgICBlbmQKPgo+ICBjbGVhbmVyIHRoYW4KPgo+ICAg
ICBpbnN0YW5jZV92YXJpYWJsZV9zZXQodmFyaWFibGVzKQoKSXQgd29uJ3QgYnV0IGhlIGRpZG4n
dCBzYXkgdGhhdC4gQW5kIEZsb3JpYW4gR2lsY2hlciBzaG93ZWQgeW91IHRoYXQKeW91IGNhbiB3
cml0ZSB5b3VyIG93biBtZXRob2QgdGhhdCBkbyBpdCBmb3IgeW91LiBJIGRvbid0IHRoaW5rIHRo
YXQKUnVieSBzaG91bGQgaGF2ZSBidWlsdC1pbiBzdWNoIGEgbWV0aG9kLiBVc2luZwppbnN0YW5j
ZV92YXJpYWJsZV9bc2ddZXQgc2hvdWxkIGJlIHZlcnkgcmFyZSAoZm9yIGV4YW1wbGUgaW4KbWV0
YXByb2dyYW1taW5nKSBhbmQgdXNpbmcgc3VjaCBhIGxvb3AgZXZlbiBtb3JlLgoKLS0gClJhZG9z
s2F3IEJ1s2F0CgpodHRwOi8vcmFkYXJlay5qb2dnZXIucGwgLSBt82ogYmxvZwo=
 
J

Joel VanderWerf

Rados³aw Bu³at said:
Does it mean that everyone use/need it? I don't think so.

Don't see what that has to do with anything. There's lots of core
methods that are rarely used. Have you used
Module:protected_method_defined? recently?
I don't know if it's main reason but AFAIR Ruby internally use
instance variables without "@" character to store additional
information about object (but from Ruby you can't get it).

That's a good point, but my suggestion was for #instance_variable_set to
map both "foo" and "@foo" to the same entry in the table, namely the
entry for "@foo". Hidden entries would remain hidden.
I think that most important is consistent. We use @ in code and the
same goes when we want get value by instance_variable_get method.

Maybe.
 
E

Eric Hodel

Just spinning wheels this evening...

Why does #instance_variable_get/set require an "@" sign in front of
the variable name? It's not like there is any other valid instance
variable name, is there?

There are, but you can only get to them from C.
 

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

No members online now.

Forum statistics

Threads
474,289
Messages
2,571,434
Members
48,120
Latest member
Natbelix

Latest Threads

Top