M
Mark Hubbart
Hi, all
Is this a bug, a feature, or just something weird?
irb(main):001:0> class Foo; def initialize(bar); @23 = bar; end; end
SyntaxError: compile error
(irb):1: `@2' is not allowed as an instance variable name
from (irb):1
irb(main):002:0> class Foo; end
=> nil
irb(main):003:0> f=Foo.new
=> #<Foo:0x46aa8>
irb(main):004:0> f.instance_variable_set@23, 23)
SyntaxError: compile error
(irb):4: `@2' is not allowed as an instance variable name
from (irb):4
irb(main):005:0> f.instance_variable_set"@23", 23)
=> 23
irb(main):006:0> f
=> #<Foo:0x46aa8 @23=23>
irb(main):007:0> RUBY_VERSION
=> "1.9.0"
@23 is an illegal instance variable. (yup)
- In line 1, I tried setting it in #initialize. didn't work, as
expected.
- line 4, tried setting using a normal symbol. again, didn't work.
- line 5, tried setting it using a quoted symbol. Somehow, it works...
I realize that this is contrived. But it *is* a little odd, isn't it?
If you do the same trick with attr_reader/attr_writer, you can create
accessor functions for the variable. "23" and "23=" show up in the
methods() array. You can call them, but only using foo.send"23=",
42).
--Mark
Is this a bug, a feature, or just something weird?
irb(main):001:0> class Foo; def initialize(bar); @23 = bar; end; end
SyntaxError: compile error
(irb):1: `@2' is not allowed as an instance variable name
from (irb):1
irb(main):002:0> class Foo; end
=> nil
irb(main):003:0> f=Foo.new
=> #<Foo:0x46aa8>
irb(main):004:0> f.instance_variable_set@23, 23)
SyntaxError: compile error
(irb):4: `@2' is not allowed as an instance variable name
from (irb):4
irb(main):005:0> f.instance_variable_set"@23", 23)
=> 23
irb(main):006:0> f
=> #<Foo:0x46aa8 @23=23>
irb(main):007:0> RUBY_VERSION
=> "1.9.0"
@23 is an illegal instance variable. (yup)
- In line 1, I tried setting it in #initialize. didn't work, as
expected.
- line 4, tried setting using a normal symbol. again, didn't work.
- line 5, tried setting it using a quoted symbol. Somehow, it works...
I realize that this is contrived. But it *is* a little odd, isn't it?
If you do the same trick with attr_reader/attr_writer, you can create
accessor functions for the variable. "23" and "23=" show up in the
methods() array. You can call them, but only using foo.send"23=",
42).
--Mark