From: James Edward Gray II Date: 2006-01-06T10:57:13+09:00 Subject: Re: user defined instance variables On Jan 5, 2006, at 5:35 PM, John Maclean wrote: > Hey Chaps, > So far so good. I've a menu as shown below. So far by following the > "pick axe" I've been able to "hard code" all of my instance > variables. I'd like a user to enter his/her details and let his > entry specify that variable? Make sense? Some idea, in code: class Inspector WELCOME = <<-END_WELCOME.gsub(/^\s+/, "").chomp Welcome to the inspector screen ~~~~~~~ ~~ ~~~ ~~~~~~~~~ ~~~~~~ 0:- Inspectors, ready to enter your details? q:- quit! enter your choice (0,q) : END_WELCOME DETAILS = %w{fname sname company dept team empno mobileno} def initialize DETAILS.each { |var| instance_variable_set("@#{var}", nil) } end def enter?( input ) case input when "0" true else false end end def build DETAILS.each do |var| answer = yield var instance_variable_set("@#{var}", answer) unless answer.empty? end end end if __FILE__ == $0 inspector = Inspector.new p inspector print Inspector::WELCOME if inspector.enter? $stdin.gets.to_s.chomp puts "Entering details capturing session" inspector.build do |var| print "#{var}: " $stdin.gets.to_s.chomp end else puts "about to quit!" end p inspector end Hopefully that gets you going. James Edward Gray II