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

Discussion in 'Ruby' started by Nicholas Orr, Jun 20, 2010.

  1. Nicholas Orr

    Nicholas Orr Guest

    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
     
    Nicholas Orr, Jun 20, 2010
    #1
    1. Advertising

  2. 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/.
     
    Brian Candler, Jun 21, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    378
    Roedy Green
    Jul 14, 2007
  2. M P
    Replies:
    1
    Views:
    485
  3. Une bévue
    Replies:
    5
    Views:
    153
    Une bévue
    Aug 10, 2006
  4. Antonio Quinonez
    Replies:
    2
    Views:
    174
    Antonio Quinonez
    Aug 14, 2003
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    110
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page