Instance_variable_set question

Hello,

Is this how this is supposed to work?

irb(main):001:0> class Foo; end
=> nil
irb(main):002:0> foo = Foo.new
=> #Foo:0x400d7528
irb(main):003:0> foo.instance_variable_set :bar, 15
NameError: bar' is not an instance variable name from (irb):3:ininstance_variable_set’
from (irb):3

This seems like it ought to work…

Chris

But the instance variable would be called :@bar :slight_smile:

···

On Tuesday, April 8, 2003, at 03:12 PM, Chris Pine wrote:

Hello,

Is this how this is supposed to work?

irb(main):001:0> class Foo; end
=> nil
irb(main):002:0> foo = Foo.new
=> #Foo:0x400d7528
irb(main):003:0> foo.instance_variable_set :bar, 15
NameError: bar' is not an instance variable name from (irb):3:in instance_variable_set’
from (irb):3

But the instance variable would be called :@bar :slight_smile:

···

----- Original Message -----
From: “Dave Thomas” dave@pragprog.com


Hah! :slight_smile:

I didn’t realize that the `@’ is just part of the name… thought it was
some sort of syntax identifier.

Perhaps the error message could be a little clearer, though. I thought it
meant, “I can’t set that variable because it doesn’t exist,” hence my
confusion.

Chris

In article F17684BC-6A02-11D7-BC62-000A95676A62@pragprog.com,

···

Dave Thomas dave@pragprog.com wrote:

On Tuesday, April 8, 2003, at 03:12 PM, Chris Pine wrote:

Hello,

Is this how this is supposed to work?

irb(main):001:0> class Foo; end
=> nil
irb(main):002:0> foo = Foo.new
=> #Foo:0x400d7528
irb(main):003:0> foo.instance_variable_set :bar, 15
NameError: bar' is not an instance variable name from (irb):3:in instance_variable_set’
from (irb):3

But the instance variable would be called :@bar :slight_smile:

It is a bit confusing, though - if you made an attr_accessor for @bar,
you refer to it as :bar, not :@bar :

class Foo
attr_accessor :bar #not :@bar
end

Phil

Hi,

···

In message “Re: instance_variable_set question” on 03/04/09, “Chris Pine” nemo@hellotree.com writes:

Perhaps the error message could be a little clearer, though. I thought it
meant, “I can’t set that variable because it doesn’t exist,” hence my
confusion.

Any concrete suggestion, please?

						matz.

Hi,

···

In message “Re: instance_variable_set question” on 03/04/09, Phil Tomson ptkwt@shell1.aracnet.com writes:

It is a bit confusing, though - if you made an attr_accessor for @bar,
you refer to it as :bar, not :@bar :

You’re specifying attribute names (i.e. exporting method names), which
does not contain “@” in them, not instance variable names.
attr_accessor defines methods that use instance variables which names
are “@”+attribute-name.

						matz.

Yukihiro Matsumoto wrote:

Hi,

Perhaps the error message could be a little clearer, though. I thought it
meant, “I can’t set that variable because it doesn’t exist,” hence my
confusion.

Any concrete suggestion, please?

  					matz.

Perhaps a small note in README.EXT? :slight_smile:

···

In message “Re: instance_variable_set question” > on 03/04/09, “Chris Pine” nemo@hellotree.com writes:

Any concrete suggestion, please?

···

----- Original Message -----
From: “Yukihiro Matsumoto” matz@ruby-lang.org


Sure.

The error was:

NameError: `bar’ is not an instance variable name

Instead, perhaps:

NameError: instance variable names must begin with `@’.

NameError: bar' is not an instance variable name; did you mean@bar’?

I prefer the second; I really like it when the interpreter makes
suggestions. When it makes a good suggestion, it’s wonderful. When it
makes a bad suggestion, it makes it much easier to see what is confusing the
interpreter.

My basic confusion was that I thought the error was saying “bar' doesn't happen to be an instance variable name in this particular object" rather than "bar’ is not allowable as an instance variable name, not now, not
ever.”

Chris

Hi,

···

At Wed, 9 Apr 2003 11:13:35 +0900, Yukihiro Matsumoto wrote:

Perhaps the error message could be a little clearer, though. I thought it
meant, “I can’t set that variable because it doesn’t exist,” hence my
confusion.

Any concrete suggestion, please?

What about these?

foo' is not proper for an instance variable name. bar’ is not recognized as an instance variable name.


Nobu Nakada

Julian Snitow wrote:

Yukihiro Matsumoto wrote:

Hi,

Perhaps the error message could be a little clearer, though. I
thought it
meant, “I can’t set that variable because it doesn’t exist,” hence my
confusion.

Any concrete suggestion, please?

                        matz.

Perhaps a small note in README.EXT? :slight_smile:

Whoops. Apparently the top thread was referring to “pure ruby”, though
it was something that also comes up when writing extensions.

Mea culpa. :slight_smile:

···

In message “Re: instance_variable_set question” >> on 03/04/09, “Chris Pine” nemo@hellotree.com writes:

Hi,

Instead, perhaps:

NameError: instance variable names must begin with `@'.

NameError: bar' is not an instance variable name; did you mean @bar’?

They are precise in your case, but:

$ ruby -e ‘instance_variable_set :“@bar…”, 15’
-e:1:in instance_variable_set': @bar…’ is not an instance variable name (NameError)
from -e:1

My basic confusion was that I thought the error was saying “bar' doesn't happen to be an instance variable name in this particular object" rather than "bar’ is not allowable as an instance variable name, not now, not
ever.”

Isn’t it fair enough?

NameError: `bar’ is not allowable as an instance variable name.

···

At Wed, 9 Apr 2003 14:01:59 +0900, Chris Pine wrote:


Nobu Nakada

Isn’t it fair enough?

NameError: `bar’ is not allowable as an instance variable name.

foo' is not proper for an instance variable name. bar’ is not recognized as an instance variable name.

···

----- Original Message -----
From: nobu.nokada@softhome.net

My basic confusion was that I thought the error was saying “bar' doesn't happen to be an instance variable name in this particular object" rather than "bar’ is not allowable as an instance variable name, not now, not
ever.”


Personally, the top one is my favorite. The second one is weird english,
and the last one has the same problem as the current error message.

NameError: `bar’ is not allowable as an instance variable name.

Sounds good to me!

Chris

Chris Pine wrote:

NameError: `bar’ is not allowable as an instance variable name.

s/allowable/allowed/

I’ve had Java interfaces ruin my original appreciation for (over)using
the suffix -able :slight_smile:

Sounds good to me!

Ditto.

···


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)