Creating trivial classes with Struct.new (adding constraints

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).

On Sep 12, 7:49 am, Rahul K. [email protected] 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.

-a