# How can I find \_\_LINE\_\_ on the execution stack?

**URL:** <https://www.ruby-forum.com/t/how-can-i-find-line-on-the-execution-stack/49583>\
**Category:** Ruby\
**Created:** [December 6, 2005, 3:20pm UTC](https://www.ruby-forum.com/t/how-can-i-find-line-on-the-execution-stack/49583 "2005-12-06T15:20:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![christer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a5b964/32.png) [@christer](https://www.ruby-forum.com/u/christer)\
**Post date:** [December 6, 2005, 3:20pm UTC](https://www.ruby-forum.com/t/how-can-i-find-line-on-the-execution-stack/49583/1 "2005-12-06T15:20:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![christer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a5b964/32.png) [@christer](https://www.ruby-forum.com/u/christer)\
**Post date:** [December 6, 2005, 3:51pm UTC](https://www.ruby-forum.com/t/how-can-i-find-line-on-the-execution-stack/49583/2 "2005-12-06T15:51:04Z")

</div>

Christer N. wrote:

> 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
```

---

<div class="post-metadata">

**Author:** ![christer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a5b964/32.png) [@christer](https://www.ruby-forum.com/u/christer)\
**Post date:** [December 6, 2005, 4:01pm UTC](https://www.ruby-forum.com/t/how-can-i-find-line-on-the-execution-stack/49583/3 "2005-12-06T16:01:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![christer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a5b964/32.png) [@christer](https://www.ruby-forum.com/u/christer)\
**Post date:** [December 6, 2005, 5:08pm UTC](https://www.ruby-forum.com/t/how-can-i-find-line-on-the-execution-stack/49583/4 "2005-12-06T17:08:32Z")

</div>

Christer N. wrote:

> > =\> 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
```

---

<div class="post-metadata">

**Author:** ![christer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a5b964/32.png) [@christer](https://www.ruby-forum.com/u/christer)\
**Post date:** [December 6, 2005, 6:06pm UTC](https://www.ruby-forum.com/t/how-can-i-find-line-on-the-execution-stack/49583/5 "2005-12-06T18:06:33Z")

</div>

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
