Putting a variable name = value format in Ruby

Posted by Calm Storm on Stack Overflow See other posts from Stack Overflow or by Calm Storm
Published on 2010-03-24T16:03:37Z Indexed on 2010/03/24 17:03 UTC
Read the original article Hit count: 165

Filed under:
|

Hi, I would like to add some debugs for my simple ruby functions and I wrote a function as below,

def debug(&block)
  varname = block.call.to_s
  puts "#{varname} = #{eval(varname,block)}"
end

debug {:x} #prints x = 5
debug {:y} #prints y = 5

I understand that eval is evil. So I have two questions.

  1. Is there any way to write that debug method without using eval? If NO is there a preferred way to do this?
  2. Is there any way to pass a list of arguments to this method? I would ideally prefer debug {:x, :y. :anynumOfvariables}. I could not quite figure out how to factor that into the debug method (i.e, to take a list of arguments)

© Stack Overflow or respective owner

Related posts about ruby

Related posts about beginner