From: Phrogz Date: 2009-03-31T05:14:54+09:00 Subject: Re: When to use instance variables @ On Mar 30, 2:05 pm, Phrogz wrote: > Class 'declarations' aren't quite what you think they are. They're > actually code that is executed. Try this on for size: Here's a more real example of why you might want to write arbitrary code while creating (or modifying) a class: class Person %w| age weight sex |.each do |param| variable_name = "@#{param}".to_sym define_method( param ) do puts "You just accessed #{param}" instance_variable_get( variable_name ) end define_method( "#{param}=" ) do |new_value| puts "Now setting #{param} to #{new_value}" instance_variable_set( variable_name, new_value ) end end end me = Person.new me.age = 42 #=> "Now setting age to 42" puts me.age #=> "You just accessed age" #=> 42