From: Brian Candler Date: 2010-06-21T21:39:41+09:00 Subject: Re: Simple way to switch between printing object key/reference and object value? Nicholas Orr wrote: > 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 > @debug = true > => true > ruby-1.8.7-p249 > puts "Name: #{@debug ? "person['name']" : > person["name"]}" > Name: person['name'] > => nil I think it would be too dangerous to override person.[]. What about when you need to access the real value? I suppose if you use person.name and keep person['name'] only for display purposes it might be OK. If this is just for debugging, I'd be inclined to override Person#inspect > DRY = ref_or_value("person['job']['position']") ?? You could do: dry = person.ref_or_value('job','position') -- Posted via http://www.ruby-forum.com/.