[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Counterpart of __LINE__ from C++ in Ruby?

RLMuller

9/13/2003 4:44:00 PM

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)
5 Answers

daz

9/13/2003 5:12:00 PM

0


"RLMuller" <RLMuller@comcast.net> 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.




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


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


daz



Rudolf Polzer

9/13/2003 5:19:00 PM

0

Scripsit illa aut ille &#187;RLMuller&#171; <RLMuller@comcast.net>:
> 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

mgarriss

9/13/2003 5:19:00 PM

0

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


RLMuller

9/14/2003 9:29:00 AM

0

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)

Daniel Kelley

9/15/2003 3:57:00 AM

0

>>>>> "Richard" == Richard <RLMuller@comcast.net> writes:

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

--
Daniel Kelley - San Jose, CA
For email, replace the first dot in the domain with an at.