Just use arrays. You can use the method Arrayto make sure you always have an array, even if someone passes only one value:
def debug(&block)
Array(block[]).each do |var| puts "#{var} = #{eval var.to_s, block}" end
end
x, y = 3, 5
debug {:x}
debug {[:x, :y]}
BTW: , Ruby 1.9. ( , , .) Proc#binding, Binding Proc:
def debug(&block)
Array(block.()).flatten.each do |var|
puts "#{var} = #{eval var.to_s, block.binding}"
end
end
, Ruby 1.8, , .
. , debug , . ?
def debug(*vars, bnd)
vars.each do |var|
puts "#{var} = #{eval var.to_s, bnd}"
end
end
x, y = 3, 5
debug :x, binding
debug :x, :y, binding
, , , callsite, . .
: - Ruby 1.9.2 (Proc#parameters):
def debug(&block)
block.parameters.map(&:last).each do |var|
puts "#{var} = #{eval var.to_s, block.binding}"
end
end
x, y = 3, 5
debug {|x|}
debug {|x, y|}