Counterpart of __LINE__ from C++ in Ruby?

R

RLMuller

This is a multi-part message in MIME format.

------=_NextPart_000_001C_01C379F4.77D53B30
Content-Type: text/plain;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

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)

------=_NextPart_000_001C_01C379F4.77D53B30
Content-Type: text/html;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Dwindows-1252">
<META content=3D"MSHTML 6.00.2800.1226" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT size=3D2>Hi All,</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Does Ruby offer a global variable like $. or a =
global method=20
that provides the line-number in which it appears as opposed to the last =
line=20
scanned by Ruby?</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>I've assembled a large script of examples from =
various Ruby=20
web sites,&nbsp; which in turn produces a lot of output.&nbsp; It'd be =
nice if I=20
could just paste in puts "#{$something}" at various points so that a =
reader=20
perusing a portion of the output could easily locate the source code =
that=20
generated it.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>It's no big deal.&nbsp; Ruby seems to have =
everything in the=20
world in it, so I thought it might have this, too, though I couldn't =
find=20
it.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Regards,<BR>Richard</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>A programmer is a device for turning coffee into =
code.<BR>Jeff=20
Prosise (with an assist from Paul Erdos)<BR></FONT></DIV></BODY></HTML>

------=_NextPart_000_001C_01C379F4.77D53B30--
 
D

daz

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.




puts __LINE__
# stuff
puts __LINE__
# I must not post in HTML
puts __LINE__


#-> 1
#-> 3
#-> 5


daz
 
M

Michael Garriss

RLMuller said:
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
 
R

Richard

Hi All,

This is a great newsgroup.

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)
 
D

Daniel Kelley

Richard> I have only one minor question: I wanted to generate a
Richard> blank line after my "Line x" line, so I ended my puts
Richard> statement with a newline, but puts acted as it it were
Richard> superfluous. I had to add \n\n to get the blank line.
Richard> Bug in Ruby?

Feature.

1046>ri IO.puts
---------------------------------------------------------------- IO#puts
ios.puts( [anObject]* ) -> nil
------------------------------------------------------------------------
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,685
Members
48,796
Latest member
Greg L.

Latest Threads

Top