Instance_variable_[gs]et

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.

Hi,

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?

Because it's part of a method name.

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

I don't think it makes programs cleaner.

              matz.

···

In message "Re: instance_variable_[gs]et" on Thu, 27 Mar 2008 13:36:23 +0900, Trans <transfire@gmail.com> writes:

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

···

On Mar 27, 2008, at 5:36 AM, Trans wrote:

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

T.

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

···

On Mar 26, 2008, at 21:36 PM, Trans wrote:

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?

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.

···

On Mar 27, 8:23 am, Florian Gilcher <f...@andersground.net> wrote:

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

On Mar 27, 2008, at 5:36 AM, Trans wrote:

> 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

Sorry matz, I don't follow. What is "it" and what method name?

T.

···

On Mar 27, 12:43 am, Yukihiro Matsumoto <m...@ruby-lang.org> wrote:

Hi,

In message "Re: instance_variable_[gs]et" > on Thu, 27 Mar 2008 13:36:23 +0900, Trans <transf...@gmail.com> writes:

>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?

Because it's part of a method name.

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

···

On 27.03.2008 16:28, Trans wrote:

On Mar 27, 8:23 am, Florian Gilcher <f...@andersground.net> wrote:

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

On Mar 27, 2008, at 5:36 AM, Trans wrote:

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

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)

It won't but he didn't say that. And Florian Gilcher showed you that
you can write your own method that do it for you. I don't think that
Ruby should have built-in such a method. Using
instance_variable_[sg]et should be very rare (for example in
metaprogramming) and using such a loop even more.

···

On Thu, Mar 27, 2008 at 4:28 PM, Trans <transfire@gmail.com> wrote:

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)

--
Radosław Bułat

http://radarek.jogger.pl - mój blog

Robert Klemme wrote:
...

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.

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...

···

On 27.03.2008 16:28, Trans wrote:

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

As a DSL implementor I could not disagree more with you, obviously
there are many people who wrote the same code
  each { |k,v|
       ivar_set "@#{k}", v

that would mean that it would be a nice feature to have, it would
simply be the most DRY solution to the problem (or DROP in this case).

Cheers
Robert

···

2008/3/27 Radosław Bułat <radek.bulat@gmail.com>:

On Thu, Mar 27, 2008 at 4:28 PM, Trans <transfire@gmail.com> wrote:

> 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)

It won't but he didn't say that. And Florian Gilcher showed you that
you can write your own method that do it for you. I don't think that
Ruby should have built-in such a method. Using
instance_variable_[sg]et should be very rare (for example in
metaprogramming) and using such a loop even more.

--
http://ruby-smalltalk.blogspot.com/

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

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.

Does it mean that everyone use/need it? I don't think so.

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

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).

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.

···

On Thu, Mar 27, 2008 at 9:07 PM, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:

--
Radosław Bułat

http://radarek.jogger.pl - mój blog

Radosław Bułat wrote:

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.

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?

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

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.

···

On Thu, Mar 27, 2008 at 9:07 PM, Joel VanderWerf > <vjoel@path.berkeley.edu> wrote:

--
        vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407