Professional Web Applications Themes

Counterpart of __LINE__ from C++ in Ruby? - Ruby

This is a multi-part message in MIME format. ------=_NextPart_000_001C_01C379F4.77D53B30 Content-Type: text/plain; cht="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 ...

  1. #1

    Default Counterpart of __LINE__ from C++ in Ruby?

    This is a multi-part message in MIME format.

    ------=_NextPart_000_001C_01C379F4.77D53B30
    Content-Type: text/plain;
    cht="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;
    cht="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; =
    cht=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--


    RLMuller Guest

  2. #2

    Default Re: Counterpart of __LINE__ from C++ in Ruby?


    "RLMuller" <RLMullercomcast.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



    daz Guest

  3. #3

    Default Re: Counterpart of __LINE__ from C++ in Ruby?

    Scripsit illa aut ille »RLMuller« <RLMullercomcast.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
    Rudolf Polzer Guest

  4. #4

    Default 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


    Michael Garriss Guest

  5. #5

    Default Re: Counterpart of LINE from C++ in Ruby?

    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)
    Richard Guest

  6. #6

    Default Re: Counterpart of LINE from C++ in Ruby?

    >>>>> "Richard" == Richard <RLMullercomcast.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.
    Daniel Kelley Guest

Similar Threads

  1. Wanted: .NET counterpart of PermissionChecker component!
    By Jens Weiermann in forum ASP.NET Security
    Replies: 1
    Last Post: February 1st, 07:11 AM
  2. Replies: 0
    Last Post: November 1st, 05:21 PM
  3. Replies: 1
    Last Post: October 29th, 07:52 PM
  4. eval and __LINE__
    By Hacksaw in forum PERL Beginners
    Replies: 7
    Last Post: September 8th, 05:45 AM
  5. [ANN] ruby-freedb, ruby-serialport, ruby-mp3info moved to Rubyforge
    By guillaume.pierronnet@ratp.fr in forum Ruby
    Replies: 0
    Last Post: August 31st, 11:57 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139