[LRUG] Dumb question: keyword arg parsing / Struct

Mr Jaba the.jaba at gmail.com
Tue Aug 10 05:08:12 PDT 2010


http://ruby-doc.org/stdlib/libdoc/ostruct/rdoc/classes/OpenStruct.html
Any good for you?

On 10 August 2010 13:08, Daniel Barlow <dan at telent.net> wrote:

> 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
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20100810/cecad7c9/attachment-0003.html>


More information about the Chat mailing list