puts by name

R

Ralph Shnelvar

[Note: parts of this message were removed to make it a legal post.]

I very often do something like:
puts "some_variable.something = #{some_variable.something}"

That repetition is getting annoying.

Is there an idiomatic Ruby way to do something like
puts_v some_variable.something
 
R

Ryan Davis

I very often do something like:
puts "some_variable.something = #{some_variable.something}"

That repetition is getting annoying.

Is there an idiomatic Ruby way to do something like
puts_v some_variable.something

Yes.

assert_equal expected, actual
 
J

John Barnette

I very often do something like:
=C2=A0puts "some_variable.something =3D #{some_variable.something}"

That repetition is getting annoying.

If you're repeating it that much, you're not writing enough tests. :)
Using `p` can save you a bit of annoyance, but not much:

p :something =3D> some_variable.something


~ j.
 
B

Brian Candler

Ralph said:
I very often do something like:
puts "some_variable.something = #{some_variable.something}"

That repetition is getting annoying.

Is there an idiomatic Ruby way to do something like
puts_v some_variable.something

Given this:

some_variable = Object.new
def some_variable.something; "hello"; end

# Option 1

def puts_v(str, b)
puts "#{str} = #{eval(str, b).inspect}"
end

puts_v "some_variable.something", binding

# Option 2, using a block to get the Binding

def puts_v(&blk)
str = yield
puts "#{str} = #{eval(str, blk).inspect}"
end

puts_v { "some_variable.something" }

# Option 2a, looks a bit odd

puts_v {%{some_variable.something}}

# Google also for Binding.of_caller
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top