Puts by name

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

On Aug 13, 2010, at 4:02 PM, Ralph S. [email protected] wrote:

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

On Fri, Aug 13, 2010 at 4:02 PM, Ralph S. [email protected]
wrote:

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

That repetition is getting annoying.

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

p :something => some_variable.something

~ j.

Ralph S. wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs