From: Mark Hubbart Date: 2004-03-03T05:20:48+09:00 Subject: bug(?) when using symbols for variables/methods 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 => # 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 => # 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