From: Michael Garriss Date: 2003-09-14T02:19:13+09:00 Subject: Re: Counterpart of __LINE__ from C++ in Ruby? RLMuller wrote: > Hi All, > > Does Ruby offer a global variable like $. or a global method that > provides the line-number in which it appears as opposed to the last > line scanned by Ruby? > > I've assembled a large script of examples from various Ruby web > sites, which in turn produces a lot of output. It'd be nice if I > could just paste in puts "#{$something}" at various points so that a > reader perusing a portion of the output could easily locate the source > code that generated it. > > It's no big deal. Ruby seems to have everything in the world in it, > so I thought it might have this, too, though I couldn't find it. > > Regards, > Richard > > A programmer is a device for turning coffee into code. > Jeff Prosise (with an assist from Paul Erdos) __LINE__ and __FILE__ work like expected in ruby. You can also use Kernel#caller to find out where you came from. This is useful you you want to write a function that will print where it was called from. This WON'T WORK: # Hmmm.....it always prints '5' def printline( linenum = __LINE__ ) puts linenum end You have to use Kernel#caller and some regexping to do that (if anyone knows an easier way please tell me). Michael