Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Simple way to switch between printing object key/reference andobject value?

Reply
Thread Tools

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

 
 
Nicholas Orr
Guest
Posts: n/a
 
      06-20-2010
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

 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      06-21-2010
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trunking between Cisco Catalyst 500 switch and other type of switch jshubo@yahoo.com Cisco 3 01-19-2008 12:10 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:52 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:50 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:28 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM



Advertisments