Ruby Banter #010

Thijs van der Vossen, 29 Apr 2008, 20:47 in ruby on rails and video (edit).

In this episode, Eloy shows how to set up a class with default attributes in a single line of code.

Manfred and Eloy

Comments

  1. Sam Aaron about 1 hour later: (delete | show email)

    Haha, fun stuff. Hopefully in a future episode you could go over the code, I think there's a lot of interesting metaprogramming stuff to be pulled out. There's some seriously crazy stuff going on in that code!

    Just for those that were wondering what happened earlier in the episode, it was defining (or in this case overriding) the initializer which caused the issues. Without it things are fine:

    <pre>
    ClassName = Struct.new(:pew)
    p ClassName.new('pew') #=> #<struct ClassName pew="pew">
    </pre>

    Also, sorry for being a spoilsport but I should probably mention OpenStruct:

    <pre>
    require 'ostruct'
    igloo = OpenStruct.new :brrr => 'cold'
    igloo.brrr #=> "cold"
    </pre>

    Anyway, when are you going to start selling Fingertips pink mugs? I think they could be really popular :-)

  2. Thijs van der Vossen about 1 hour later: (delete)

    You can get your very own pink mugs from the HEMA at http://tinyurl.com/6xnusp

  3. Eloy Duran 3 days later: (delete)

    @Sam: While OpenStruct is in practice not completely the same, for instance default values, our code might however be simplified by using OpenStruct as a base class.

    Glad that you enjoyed it!

    And yes the mugs are a "toppertje" ;)

Add your comment

In order to fight spam on this blog, posting comments from a browser without javascript is currently not supported.