Re: WTF is Struct? Oh! [was Re: Ruby beginner question 2]
Gavin Kistner <[email protected]>
| Newsgroups | gmane.comp.lang.ruby.documentation |
|---|---|
| Message-ID | <[email protected]> |
On Aug 22, 2005, at 6:47 AM, Alexander Kellett wrote: > yup. though don't be caught by this: > > irb(main):003:0> B=Struct.new :g > => B > irb(main):004:0> class B; def blah; p @g; end; end > => nil > irb(main):005:0> B.new(5).blah > nil > => nil Yeah, as I was writing up my suggested example for the docs, I noticed that the properties weren't available as instance variables, and I needed to use the accessor methods instead to read the values. Indeed, it looks like instances of the Struct somehow magically store their internal state in the ether: irb(main):001:0> Klass = Struct.new( :foo, :bar ) => Klass irb(main):002:0> instance = Klass.new( 'f', 'b' ) => #<struct Klass foo="f", bar="b"> irb(main):003:0> instance.instance_variables => [] Is this is a case where some internal details are being magically hidden by the C side? Or is it possible in Ruby to have an instance variable that doesn't show up in #instance_variables?