From: Nicholas Orr Date: 2010-06-21T07:28:10+09:00 Subject: Simple way to switch between printing object key/reference and object value? Hi, I have a hash: ruby-1.8.7-p249 > person = {"name"=>"nick","age"=>26,"country"=>"australia","job"=>{"position"=>"it guy","start"=>"2010-01-01"},"roles"=>["admin","user","login"]} => {"name"=>"nick", "country"=>"australia", "job"=>{"position"=>"it guy", "start"=>"2010-01-01"}, "roles"=>["admin", "user", "login"], "age"=>26} I have an instance variable: @debug when @debug is true I should get the key/reference that is being called, when it is false I should get the value, like this ruby-1.8.7-p249 > @debug = false => false ruby-1.8.7-p249 > puts "Name: #{@debug ? "person['name']" : person["name"]}" Name: nick => nil ruby-1.8.7-p249 > puts "Position: #{@debug ? "person['job']['position']" : person["job"]["position"]}" Position: it guy => nil ruby-1.8.7-p249 > @debug = true => true ruby-1.8.7-p249 > puts "Name: #{@debug ? "person['name']" : person["name"]}" Name: person['name'] => nil ruby-1.8.7-p249 > puts "Position: #{@debug ? "person['job']['position']" : person["job"]["position"]}" Position: person['job']['position'] => nil This works great for one of things - typing out the if @debug..then..else - however I need to do this all the time and I'm looking for a way to do it automatically or DRY. a simple method that looks at the @debug state then either outputs the value or the reference, is it possible or do I just have to do it the way I've shown above? The person hash is always called person and is also an instance variable if that helps... DRY = ref_or_value("person['job']['position']") ?? Thanks, Nick