creating trivial classes with Struct.new (adding constraints

Discussion in 'Ruby' started by Rahul Kumar, Sep 12, 2010.

  1. Rahul Kumar

    Rahul Kumar Guest

    I'd like to use this form for making small classes rather than hand
    coding my "beans".

    ActionEvent = Struct.new:)source, :id, :text) do
    def xxx ... end
    end

    However, is there anyway I can add some constraints, such as:

    - source (some of the fields) must not be nil.
    - no changing values after creation, unless there's a method provided

    Essentially, the fields should correspond to attr_reader and not
    attr_accessor.
    Have i lost the open purpose of this form in my requirement.

    I was reading this article about it:
    http://blog.grayproductions.net/articles/all_about_struct (Thx to JEG).
    --
    Posted via http://www.ruby-forum.com/.
     
    Rahul Kumar, Sep 12, 2010
    #1
    1. Advertising

  2. Rahul Kumar

    ara.t.howard Guest

    On Sep 12, 7:49=A0am, Rahul Kumar <> wrote:

    > Essentially, the fields should correspond to attr_reader and not
    > attr_accessor.
    > Have i lost the open purpose of this form in my requirement.



    http://github.com/ahoward/openobject

    -a
     
    ara.t.howard, Sep 13, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Juan Carlos Allica
    Replies:
    2
    Views:
    680
    Noway2
    Jan 25, 2006
  2. HK
    Replies:
    0
    Views:
    436
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,405
    Chris Fogelklou
    Apr 20, 2004
  4. Replies:
    5
    Views:
    379
  5. baibaichen

    trivial or non-trivial object

    baibaichen, Jan 12, 2006, in forum: C++
    Replies:
    3
    Views:
    926
    osmium
    Jan 12, 2006
Loading...

Share This Page