# Simple way to switch between printing object key/reference and object value?

**URL:** <https://www.ruby-forum.com/t/simple-way-to-switch-between-printing-object-key-reference-and-object-value/190463>\
**Category:** Ruby\
**Created:** [June 21, 2010, 12:28am UTC](https://www.ruby-forum.com/t/simple-way-to-switch-between-printing-object-key-reference-and-object-value/190463 "2010-06-21T00:28:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![soregums](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e36b37/32.png) [@soregums](https://www.ruby-forum.com/u/soregums)\
**Post date:** [June 21, 2010, 12:28am UTC](https://www.ruby-forum.com/t/simple-way-to-switch-between-printing-object-key-reference-and-object-value/190463/1 "2010-06-21T00:28:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![soregums](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e36b37/32.png) [@soregums](https://www.ruby-forum.com/u/soregums)\
**Post date:** [June 21, 2010, 2:39pm UTC](https://www.ruby-forum.com/t/simple-way-to-switch-between-printing-object-key-reference-and-object-value/190463/2 "2010-06-21T14:39:39Z")

</div>

Nicholas O. 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’)
