From: Chris Morris Date: 2002-08-09T05:57:54+09:00 Subject: Variable validation I've got a class that has a number of instance variables, some of which must be set prior to calling a save function to write these off somewhere else. I'm doing the following: def check_required if @password == nil or @password.empty? or @firstName == nil or @firstName.empty? or @lastName == nil or @lastName.empty? or @nickName == nil or @nickName.empty? or @email == nil or @email.empty? raise RequiredDataMissingError('') end ... but this smells. I thought about putting them all in an array, then iterating over it -- one downside is I can only put them in an array for the duration of the call because: irb(main):001:0> @a = 'hey' "hey" irb(main):002:0> @b = 'you' "you" irb(main):003:0> vars = [@a, @b] ["hey", "you"] irb(main):004:0> @a = 'yo' "yo" irb(main):005:0> vars ["hey", "you"] ... shows that there's not a link kept between the @a in vars and @a. I guess this is better: def check_required vars = { 'password' => @password, 'firstName' => @firstName, 'lastName' => @lastName, 'nickName' => @nickName, 'email' => @email, } missing = '' vars.each do |name, value| missing << ', ' if !missing.empty? missing << name if value == nil || value.empty? end raise RequiredDataMissingError.new("Required fields not set: #{missing}") if !missing.empty? end Any better ideas? Chris http://clabs.org