From: Raul Parolari Date: 2007-11-12T14:40:25+09:00 Subject: Re: Controlling attribute setting in a Struct Uups, I treated the maximum as the "first invalid value"; correction: class MyMaxBoundedStruct < Struct.new(:life, :strength, :charisma, :weapon) # class variable (depending on Spec, a Constant could be used) @@maximum = 1000 arr = %w[life strength charisma weapon] arr.each do |m| define_method("#{m}=") do |val| if val <= @@maximum super(val) else raise ArgumentError, "#{m} set above maximum" end end end end # class rabbit = MyMaxBoundedStruct.new rabbit.strength = 1000 # ok rabbit.strength = 1001 # Argument: strength set above maximum -- Posted via http://www.ruby-forum.com/.