creating trivial classes with Struct.new (adding constraints

R

Rahul Kumar

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top