0

When a call is made to a method, you can get the line number of the command where the call was made:

loc = caller_locations(1, 1).first
loc.lineno # => 3 (or whatever the line number is)

I use that information for a little home brewed debugging tool I use.

It would be nice (though not required) if I could get the total number of lines in the calling file. That way I could output something like this:

3/122

It wouldn't be worth the cost of manually counting up the number of lines in the file by using loc.path. Does Ruby provide a cheap way to get the line count?

tscheingeld
  • 789
  • 7
  • 24
  • 2
    You can try a [simple implementation](https://stackoverflow.com/questions/2650517/count-the-number-of-lines-in-a-file-without-reading-entire-file-into-memory) as shown there. Being `caller_locations` a `Kernel` method, it doesn't seem to be the case that Ruby has that built-in in the way you want. – Pipetus May 16 '23 at 04:14
  • The method `caller` returns an array, where you see the filename of the caller and the line number. You can open the file and count the lines. – user1934428 May 16 '23 at 06:56

0 Answers0