Discussion:
instance_variable_set question
Chris Pine
2003-04-08 20:12:18 UTC
Permalink
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


This seems like it ought to work...


Chris
Dave Thomas
2003-04-08 20:44:44 UTC
Permalink
Post by Chris Pine
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 :)
Chris Pine
2003-04-08 21:16:26 UTC
Permalink
----- Original Message -----
From: "Dave Thomas" <***@pragprog.com>

But the instance variable would be called :@bar :)
----------------------------

Hah! :)

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.

<chuckle>

Chris
Yukihiro Matsumoto
2003-04-09 02:13:35 UTC
Permalink
Hi,

In message "Re: instance_variable_set question"
on 03/04/09, "Chris Pine" <***@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.
Julian Snitow
2003-04-09 04:37:40 UTC
Permalink
Post by Yukihiro Matsumoto
Hi,
In message "Re: instance_variable_set question"
|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? :-)
Whoops. Apparently the top thread was referring to "pure ruby", though
it was something that also comes up when writing extensions.

Mea culpa. :)
Julian Snitow
2003-04-09 04:37:34 UTC
Permalink
Post by Yukihiro Matsumoto
Hi,
In message "Re: instance_variable_set question"
|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? :-)
Chris Pine
2003-04-09 05:01:59 UTC
Permalink
----- Original Message -----
From: "Yukihiro Matsumoto" <***@ruby-lang.org>

Any concrete suggestion, please?
----------------------------

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
n***@softhome.net
2003-04-09 05:17:52 UTC
Permalink
Hi,

At Wed, 9 Apr 2003 14:01:59 +0900,
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
Post by Chris Pine
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.
--
Nobu Nakada
Chris Pine
2003-04-09 14:11:25 UTC
Permalink
----- Original Message -----
Post by Chris Pine
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.

<other email>

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

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
Kent Dahl
2003-04-09 17:00:46 UTC
Permalink
Post by n***@softhome.net
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 :-)
Post by n***@softhome.net
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___)
n***@softhome.net
2003-04-09 05:04:16 UTC
Permalink
Hi,

At Wed, 9 Apr 2003 11:13:35 +0900,
Post by Yukihiro Matsumoto
|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
Phil Tomson
2003-04-09 07:58:02 UTC
Permalink
Post by Chris Pine
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
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
Yukihiro Matsumoto
2003-04-09 08:31:56 UTC
Permalink
Hi,

In message "Re: instance_variable_set question"
on 03/04/09, Phil Tomson <***@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.
Loading...