puts by name

Discussion in 'Ruby' started by Ralph Shnelvar, Aug 14, 2010.

  1. [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
     
    Ralph Shnelvar, Aug 14, 2010
    #1
    1. Advertising

  2. Ralph Shnelvar

    Ryan Davis Guest

    On Aug 13, 2010, at 4:02 PM, Ralph Shnelvar <> 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
     
    Ryan Davis, Aug 14, 2010
    #2
    1. Advertising

  3. On Fri, Aug 13, 2010 at 4:02 PM, Ralph Shnelvar <> wrote:
    > 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.
     
    John Barnette, Aug 14, 2010
    #3
  4. Ralph Shnelvar 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
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Aug 16, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    7
    Views:
    876
    Roedy Green
    Jan 2, 2006
  2. ding feng
    Replies:
    2
    Views:
    2,876
    ding feng
    Jun 25, 2003
  3. Bobby Chamness
    Replies:
    2
    Views:
    2,426
    Joe Smith
    Apr 22, 2007
  4. Michael Brooks
    Replies:
    22
    Views:
    434
    Rick DeNatale
    Mar 27, 2007
  5. Raj Singh
    Replies:
    4
    Views:
    153
    Ryan Ingram
    Jan 29, 2008
Loading...

Share This Page