0

I have

a = 'a string'
b = 3

I use this common pattern:

puts "a: #{a}, b: #{a}"

I would like to do this with a method e.g.,

def puts_auto(*args)
  str = args.map { |arg| "#{arg.object_name #{a}" }.join('')
  puts str
end

where object_name finds the name of the object that was used to create the arg. Is there such method like object_name? Is there a way to print the value and the name of each argument from a splat argument list?

Obromios
  • 15,408
  • 15
  • 72
  • 127

1 Answers1

0

Using Mark's suggestion in regard to this SO question, the following works

  def puts_auto (*args, &b)
    str = args.map { |arg| "#{arg} = #{eval(arg.to_s, b.binding)}" }.join(', ')
    puts str
  end

so

puts_auto(:a, :b){} # outputs `a = a string, b = 3`
Obromios
  • 15,408
  • 15
  • 72
  • 127