Re: WTF is Struct? Oh! [was Re: Ruby beginner question 2]
Alexander Kellett <[email protected]>
| Newsgroups | gmane.comp.lang.ruby.documentation |
|---|---|
| Message-ID | <[email protected]> |
On Aug 22, 2005, at 2:37 PM, Gavin Kistner wrote: > On Aug 22, 2005, at 2:31 AM, Robert Klemme wrote: >> A simpler alternative is to just use Struct: >> >> # direct >> Song = Struct.new :name, :artist, :duration >> >> class Song >> # other methods >> end > > Wait wait wait...I never grokked Struct before. > > Is it really just a convenient factory for generating classes with > a set of accessor properties, and a constructor that accepts zero > or more of those properties in order? 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 i frequently write quick Struct's and later refactor them into real classes. Alex