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)
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).
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.
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?
def foo()
puts “foo() at #{caller(0)[0]}”
end
foo()
puts “main program at #{caller(0)[0]}”
BTW, it works exactly the same way as in Perl, just better (also
reporting correct values when called from the main program, not
only from subroutines).
···
–
The only thing that Babelfish is good at is proving that Vogons are
great poets.
Josef ‘Jupp’ Schugt in japan.anime.evangelion
Thank you all for your responses. I’m using the code below, which
works great for my purposes. I will look into Kernel#caller when I
get a chance.
I have only one minor question: I wanted to generate a blank line
after my “Line x” line, so I ended my puts statement with a newline,
but puts acted as it it were superfluous. I had to add \n\n to get
the blank line. Bug in Ruby?
–CODE
def foo()
puts "Subroutine foo at #{caller(0)[0]}"
end
puts "Line #{LINE}\n"
foo()
puts “main program at #{caller(0)[0]}”
–END
Regards,
Richard
A programmer is a device for turning coffee into code.
Jeff Prosise (with an assist from Paul Erdos)
I have only one minor question: I wanted to generate a
blank line after my “Line x” line, so I ended my puts
statement with a newline, but puts acted as it it were
superfluous. I had to add \n\n to get the blank line.
Bug in Ruby?
Writes the given objects to ios as with IO#print. Writes a record
separator (typically a newline) after any that do not already end
with a newline sequence. If called with an array argument, writes
each element on a new line. If called without arguments, outputs a
single record separator.
$stdout.puts("this", "is", "a", "test")
produces:
this
is
a
test
–
Daniel Kelley - San Jose, CA
For email, replace the first dot in the domain with an at.