[LRUG] Dumb question: keyword arg parsing / Struct

Daniel Barlow dan at telent.net
Tue Aug 10 05:08:31 PDT 2010


OK, I'm sure this is one of those things that everyone else knows about 
except me[*], but Googling is getting me nowhere.

Is there a gem or a standard idiom for creating Struct-like classes 
whose #new method accepts named parameters (corresponding to the 
accessor names) instead of arguments by order?  I would like to write, 
for example

  class Book < NamedStruct.new(:isbn,:author,:title,:review_rating)
  b=Book.new(:author=>'Ian McDonald',:title=>'Brasyl')

  # and in approximately rspec syntax
  b.title.should == Brasyl'
  b.review_rating.should be_nil

If there isn't then I could probably stand to improve my understanding 
of Ruby metaprogramming significantly by writing my own, but it seems 
like the kind of wheel that must have been reinvented many times 
already, and surely by now someone has made a nice smooth round one



-dan

[*] like Timecop ... thanks Time & Chris for mentioning it last night



More information about the Chat mailing list