How can I find __LINE__ on the execution stack ?

Discussion in 'Ruby' started by Christer Nilsson, Dec 6, 2005.

  1. I would like to be able to display the line number in my simple assert.

    1 def assert(expect, actual, *msg)
    2 expect==actual ? print(".") : print("\nAssert at line #{__LINE__}
    failed: #{expect.inspect} <> #{actual.inspect} #{msg}\n")
    3 end
    4
    5 assert 1, 2
    6 assert 1, 1

    The solution above erroneously displays line 2 instead of line 5.

    Christer

    --
    Posted via http://www.ruby-forum.com/.
    Christer Nilsson, Dec 6, 2005
    #1
    1. Advertising

  2. Christer Nilsson wrote:
    > I would like to be able to display the line number in my simple
    > assert.
    >
    > 1 def assert(expect, actual, *msg)
    > 2 expect==actual ? print(".") : print("\nAssert at line #{__LINE__}
    > failed: #{expect.inspect} <> #{actual.inspect} #{msg}\n")
    > 3 end
    > 4
    > 5 assert 1, 2
    > 6 assert 1, 1
    >
    > The solution above erroneously displays line 2 instead of line 5.


    You can use caller[0] to get the info you want:

    irb(main):007:0> def test
    irb(main):008:1> p caller[0]
    irb(main):009:1> end
    => nil
    irb(main):010:0> test
    "(irb):10:in `irb_binding'"
    => nil

    Kind regards

    robert
    Robert Klemme, Dec 6, 2005
    #2
    1. Advertising

  3. bob.news wrote:

    > You can use caller[0] to get the info you want:
    >
    > irb(main):007:0> def test
    > irb(main):008:1> p caller[0]
    > irb(main):009:1> end
    > => nil
    > irb(main):010:0> test
    > "(irb):10:in `irb_binding'"
    > => nil
    >
    > Kind regards
    >
    > robert


    Thank you Robert!

    def assert(expect, actual, *msg)
    expect==actual ? print(".") : print("\nAssert failed:
    #{expect.inspect} <> #{actual.inspect} in line
    #{caller[0].split('\\').last} #{msg}\n")
    end

    Christer

    --
    Posted via http://www.ruby-forum.com/.
    Christer Nilsson, Dec 6, 2005
    #3
  4. Christer Nilsson wrote:
    > bob.news wrote:
    >
    >> You can use caller[0] to get the info you want:
    >>
    >> irb(main):007:0> def test
    >> irb(main):008:1> p caller[0]
    >> irb(main):009:1> end
    >> => nil
    >> irb(main):010:0> test
    >> "(irb):10:in `irb_binding'"
    >> => nil
    >>
    >> Kind regards
    >>
    >> robert

    >
    > Thank you Robert!


    You're welcome.

    > def assert(expect, actual, *msg)
    > expect==actual ? print(".") : print("\nAssert failed:
    > #{expect.inspect} <> #{actual.inspect} in line
    > #{caller[0].split('\\').last} #{msg}\n")
    > end


    Btw, one way to make your assertion code maybe a bit more runtime savvy
    would be to use blocks:

    if do_test?
    def assert()
    raise AssertionError unless yield
    end
    else
    def assert() end
    end

    assert { foo > 10 }

    IIRC there is already an assert method that works exactly this way.

    Kind regards

    robert
    Robert Klemme, Dec 6, 2005
    #4
  5. bob.news wrote:
    > if do_test?
    > def assert()
    > raise AssertionError unless yield
    > end
    > else
    > def assert() end
    > end
    >
    > assert { foo > 10 }
    >
    > IIRC there is already an assert method that works exactly this way.
    >
    > Kind regards
    >
    > robert


    Robert, I guess this is a minimalistic approach. I will see the info I
    need in the uncatched error.

    I introduced my own assert, because of the timings. But since I switched
    to ArachnoRuby there is no need to avoid test/unit.

    Anyway, your trick can be handy too. I'll save it in my tool chest.

    Christer

    --
    Posted via http://www.ruby-forum.com/.
    Christer Nilsson, Dec 6, 2005
    #5
    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. Imre
    Replies:
    4
    Views:
    619
    Peter Gordon
    Feb 23, 2005
  2. Replies:
    2
    Views:
    477
    Raymond Martineau
    Feb 23, 2005
  3. Replies:
    5
    Views:
    318
    Keith Thompson
    Mar 12, 2005
  4. Carlos

    Can __LINE__ be stringified?

    Carlos, Jul 29, 2005, in forum: C Programming
    Replies:
    5
    Views:
    515
    Michael Wojcik
    Aug 1, 2005
  5. David Tran
    Replies:
    2
    Views:
    71
    David Tran
    Sep 15, 2005
Loading...

Share This Page