Parsing the Ruby File

Discussion in 'Ruby' started by Thillai S., Dec 30, 2010.

  1. Thillai S.

    Thillai S. Guest

    Hai any one pls guide me...
    We can extract the line number of the sub routine/function/method in a
    Ruby file using the ctags command.

    But I want to know the line number in which the sub routine is ending.

    Example:

    Quote:
    #
    # Function name : get_days_in_hash
    # Argument :
    # no arguments
    # Return value :
    # days information in hash format.
    # Description :
    # get the days. Assigned value for each day.
    # 1 => 'Mon' ,
    # 2 => 'Tues' ,
    # 3 => 'Wed' ,
    # 4 => 'Thu' ,
    # 5 => 'Fri' ,
    # 6 => 'Sat' ,
    # 7 => 'Sun' ,
    # 8 => 'Leave' ,
    # 9 => 'Holiday'
    # Logical flow :
    # generate the hash
    #
    def get_days_in_hash
    { 1 => 'Mon' , 2 => 'Tues' , 3 => 'Wed' , 4 => 'Thu' , 5 => 'Fri' , 6 =>
    'Sat' , 7 => 'Sun' , 8 => 'Leave' , 9 => 'Holiday' }
    end
    Quote:
    #
    # Function name : user_filter
    # Argument :
    # get the controller and action.
    # Return value :
    # if the user not login then display the login page.
    # Description :
    # check the user is login or not.
    # Logical flow :
    # check the controller and action is not the login page or not.
    # check the user is log-in or not.
    # if not login then redirect the page to login page.
    #
    def user_filter()

    # set the header information for the Browser
    no_cache()

    # check it is a login page or not.
    # If it is not a not the login page then check the session whether he is
    login or not
    # if is not login then display the login page.
    if( not ( params[:controller] == 'main' && ( params[:action] == 'login'
    || params[:action] == 'index' ) ) )
    if( not session[:user_id] )
    #redirect_to :controller => 'main' , :action => 'login'
    if( params[:action] == 'logout' )
    flash[:notice] = 'Logged out Successfully'
    else
    flash[:notice] = "Session Expired"
    end
    render :text => "<script>
    window.location='#{url_for:)controller => 'main' , :action => 'login'
    )}';
    </script>"
    end
    end
    end
    Say the function get_days_in_hash starts in the line number 20.
    It will be returned by the ctags command.
    I want to know in which line number that appropriate function is ending.

    For each functions in the file I want this informations like starting
    line of the file and the ending line of the file.

    Please help me about how to achieve this.
    Thanks in Advance,

    --
    Posted via http://www.ruby-forum.com/.
     
    Thillai S., Dec 30, 2010
    #1
    1. Advertising

  2. Thillai S.

    Ryan Davis Guest

    On Dec 30, 2010, at 2:56, "Thillai S." <> wrote:

    > But I want to know the line number in which the sub routine is ending


    What are you actually trying to do?
     
    Ryan Davis, Dec 30, 2010
    #2
    1. Advertising

  3. Thillai S.

    Thillai S. Guest

    Ryan Davis wrote in post #971438:
    > On Dec 30, 2010, at 2:56, "Thillai S." <> wrote:
    >
    >> But I want to know the line number in which the sub routine is ending

    >
    > What are you actually trying to do?


    Thanks for ur reply!

    Say for example a sub routine definition is starting in the line number
    15
    The end of the sub routine definition is there is some line x.
    I want to know the line number x through a script.

    Example

    consider the ruby program

    #!/usr/bin/ruby #first line of the program
    #################################################################################
    # DESCRIPTION : xxxxxxxx
    # AUTHOR : yyyyyyyy
    #################################################################################

    # a sample test function
    def test #8th line of the program
    xxxxxxxxxx; #9th line
    yyyyyyyyyy; #10th line
    end #11th line -- function test ends here



    Here the function test defition starts in the line number 8.
    The ending line test function is 11.

    The ctags -x <program_name>
    will return the starting line number of the testing function like,
    test subroutine 8 file_name.pl sub test()

    So from the output of the ctags command I can get the line number of the
    test function definition starting.

    Similarly I want to get the ending line number of the sub routine.

    How can we achieve this?
    Please help me...

    --
    Posted via http://www.ruby-forum.com/.
     
    Thillai S., Dec 31, 2010
    #3
  4. Thillai S.

    Andrew Ting Guest

    Does this help?

    print __LINE__ + 1


    > #!/usr/bin/ruby #first line of the program
    >

    #################################################################################
    > # DESCRIPTION : xxxxxxxx
    > # AUTHOR : yyyyyyyy
    >

    #################################################################################
    >
    > # a sample test function
    > def test #8th line of the program
    > xxxxxxxxxx; #9th line
    > yyyyyyyyyy; #10th line

    print __LINE__ + 1
    > end #11th line -- function test ends here
    >


    --
    Posted via http://www.ruby-forum.com/.
     
    Andrew Ting, Dec 31, 2010
    #4
  5. Thillai S.

    Ryan Davis Guest

    On Dec 30, 2010, at 20:40, "Thillai S." <> wrote:

    > Ryan Davis wrote in post #971438:
    >> On Dec 30, 2010, at 2:56, "Thillai S." <> wrote:
    >>=20
    >>> But I want to know the line number in which the sub routine is ending

    >>=20
    >> What are you actually trying to do?

    >=20
    > Thanks for ur reply!
    >=20
    > Say for example a sub routine definition is starting in the line number=20=


    > 15
    > The end of the sub routine definition is there is some line x.
    > I want to know the line number x through a script.



    That didn't answer my question, it merely repeated your original problem sta=
    tement.

    Why do you want that? What are you trying to achieve?=
     
    Ryan Davis, Dec 31, 2010
    #5
  6. Thillai S.

    Thillai S. Guest

    I want to find the number of lines in the function code.
    And to find the number of comment lines with in that function
    definition.
    I am doing code quality analysis.
    For that only I want to know the end line number.
    So that I can get the total number of lines there in that functions.
    If there are 40 lines of code in the function, then it should have
    minimum 20 lines of comments for that function.

    Thats why I want to know the end line number of the function

    --
    Posted via http://www.ruby-forum.com/.
     
    Thillai S., Dec 31, 2010
    #6
  7. Thillai S.

    Ryan Davis Guest

    On Dec 31, 2010, at 00:16 , Thillai S. wrote:

    > If there are 40 lines of code in the function, then it should have=20
    > minimum 20 lines of comments for that function.


    you're kidding right? quality (of _any_ kind) can _not_ be determined by =
    the ratio of comment to code.

    For example (from your own example):

    > #
    > # Function name : get_days_in_hash
    > # Argument :
    > # no arguments
    > # Return value :
    > # days information in hash format.
    > # Description :
    > # get the days. Assigned value for each day.
    > # 1 =3D> 'Mon' ,
    > # 2 =3D> 'Tues' ,
    > # 3 =3D> 'Wed' ,
    > # 4 =3D> 'Thu' ,
    > # 5 =3D> 'Fri' ,
    > # 6 =3D> 'Sat' ,
    > # 7 =3D> 'Sun' ,
    > # 8 =3D> 'Leave' ,
    > # 9 =3D> 'Holiday'
    > # Logical flow :
    > # generate the hash
    > #
    > def get_days_in_hash
    > { 1 =3D> 'Mon' , 2 =3D> 'Tues' , 3 =3D> 'Wed' , 4 =3D> 'Thu' , 5 =3D> =

    'Fri' , 6 =3D>
    > 'Sat' , 7 =3D> 'Sun' , 8 =3D> 'Leave' , 9 =3D> 'Holiday' }
    > end


    That apparently has GREAT quality as the ratio is 20:3 (depending on how =
    you count it)... That's much much much better than the 1:2 ratio you're =
    setting as your bar, right? Right??

    So, by that same logic, the following would be 20:13, that's down to =
    ~3/2, not nearly as good:

    > #
    > # Function name : get_days_in_hash
    > # Argument :
    > # no arguments
    > # Return value :
    > # days information in hash format.
    > # Description :
    > # get the days. Assigned value for each day.
    > # 1 =3D> 'Mon' ,
    > # 2 =3D> 'Tues' ,
    > # 3 =3D> 'Wed' ,
    > # 4 =3D> 'Thu' ,
    > # 5 =3D> 'Fri' ,
    > # 6 =3D> 'Sat' ,
    > # 7 =3D> 'Sun' ,
    > # 8 =3D> 'Leave' ,
    > # 9 =3D> 'Holiday'
    > # Logical flow :
    > # generate the hash
    > #
    > def get_days_in_hash
    > {
    > 1 =3D> 'Mon',
    > 2 =3D> 'Tues',
    > 3 =3D> 'Wed',
    > 4 =3D> 'Thu',
    > 5 =3D> 'Fri',
    > 6 =3D> 'Sat',
    > 7 =3D> 'Sun',
    > 8 =3D> 'Leave',
    > 9 =3D> 'Holiday',
    > }
    > end


    But it's the same exact code!

    Now let's look at your actual comment block:

    #
    # Function name : get_days_in_hash # we already know it.
    # Argument : # we already know it.
    # no arguments
    # Return value : # described in name
    # days information in hash format.
    # Description :
    # get the days. Assigned value for each day. # also described in name
    # 1 =3D> 'Mon' , # EXACT DUPE OF CODE
    # 2 =3D> 'Tues' ,
    # 3 =3D> 'Wed' ,
    # 4 =3D> 'Thu' ,
    # 5 =3D> 'Fri' ,
    # 6 =3D> 'Sat' ,
    # 7 =3D> 'Sun' ,
    # 8 =3D> 'Leave' ,
    # 9 =3D> 'Holiday'
    # Logical flow :
    # generate the hash # no clue what this is =
    for
    #

    I'd have written the comment as:

    ##
    # Returns a hash mapping the day code (including leave and holiday) to =
    its name.

    That drops me from 20:13 to 2:13. That makes mine bad, right?

    Before I knew what you were trying to do, I would have probably =
    recommended looking at ruby_parser to figure out your line number =
    problem. But now that I know, I wouldn't. It has like... almost NO =
    comments:

    Classes: 12 ( 9 undocumented)
    Constants: 29 ( 28 undocumented)
    Modules: 0 ( 0 undocumented)
    Methods: 466 ( 417 undocumented)

    and lots and lots of code:

    % find lib -type f | xargs wc -l
    120 lib/gauntlet_rubyparser.rb
    1307 lib/ruby_lexer.rb
    1790 lib/ruby_parser.y
    1030 lib/ruby_parser_extras.rb
    4247 total
     
    Ryan Davis, Dec 31, 2010
    #7
  8. Thillai S.

    Thillai S. Guest

    > you're kidding right? quality (of _any_ kind) can _not_ be determined by
    > the ratio of comment to code.
    >
    > For example (from your own example):
    >


    >
    > That apparently has GREAT quality as the ratio is 20:3 (depending on how
    > you count it)... That's much much much better than the 1:2 ratio you're
    > setting as your bar, right? Right??
    >
    > So, by that same logic, the following would be 20:13, that's down to
    > ~3/2, not nearly as good:
    >
    > But it's the same exact code!
    >
    > Now let's look at your actual comment block:
    >
    >
    > I'd have written the comment as:
    >
    > ##
    > # Returns a hash mapping the day code (including leave and holiday) to
    > its name.
    >
    > That drops me from 20:13 to 2:13. That makes mine bad, right?
    >
    > Before I knew what you were trying to do, I would have probably
    > recommended looking at ruby_parser to figure out your line number
    > problem. But now that I know, I wouldn't. It has like... almost NO
    > comments:
    >



    Kindly don't deviate the topic.
    I want to extract only the function code.
    Tell me how can we extract only the function code.
    How to identify in which line the function is ending.
    I want to extract like this for each functions in a ruby file.

    --
    Posted via http://www.ruby-forum.com/.
     
    Thillai S., Dec 31, 2010
    #8
  9. Thillai S.

    Thillai S. Guest

    You have asked for what purpose y want to achieve this. For that only I
    explained the purpose.
    So don't think about what I am going to do with this.
    Kindly provide solutions for my requirement

    --
    Posted via http://www.ruby-forum.com/.
     
    Thillai S., Dec 31, 2010
    #9
  10. Thillai S.

    Ryan Davis Guest

    On Dec 31, 2010, at 02:39 , Thillai S. wrote:

    >>
    >> Before I knew what you were trying to do, I would have probably
    >> recommended looking at ruby_parser to figure out your line number
    >> problem. But now that I know, I wouldn't. It has like... almost NO
    >> comments:

    >
    > Kindly don't deviate the topic.
    > I want to extract only the function code.
    > Tell me how can we extract only the function code.
    > How to identify in which line the function is ending.
    > I want to extract like this for each functions in a ruby file.


    kindly read my response.
     
    Ryan Davis, Dec 31, 2010
    #10
  11. >you're kidding right? quality (of _any_ kind) can _not_ be determined by
    >the ratio of comment to code.


    Having said that, the more the comment there, is the better. I don't
    like this idea that if you right clever compact code, other people will
    immediately get what you're trying to do. Why make the program a puzzle?
    If I want a challenge to pass the time, I can choose a crossword.

    Thillai S, where did you get this 2:1 ration from - I must admit I've
    never heard of anything like this before?

    --
    Posted via http://www.ruby-forum.com/.
     
    Mike Stephens, Dec 31, 2010
    #11
  12. Thillai S.

    Ryan Davis Guest

    On Dec 31, 2010, at 2:48, Mike Stephens <> wrote:

    >> you're kidding right? quality (of _any_ kind) can _not_ be determined by
    >> the ratio of comment to code.

    >=20
    > Having said that, the more the comment there, is the better.


    Then you obviously approve of his code comment having a copy of the code in i=
    t. More is better, like you said... By extension TWO copies would be even be=
    tter.

    Boy do I have a text editor to sell to you! You'll LOVE what it'll do to "im=
    prove" your code quality.=
     
    Ryan Davis, Dec 31, 2010
    #12
  13. Thillai S.

    Thillai S. Guest

    Mike Stephens wrote in post #971626:
    In my organization we are following this standard.
    A program should have one line comment per 2 lines of source code.
    Kindly tell me the perl script to extract only the function code!

    --
    Posted via http://www.ruby-forum.com/.
     
    Thillai S., Dec 31, 2010
    #13
  14. Thillai S.

    Thillai S. Guest

    Ryan Davis wrote in post #971603:

    > % find lib -type f | xargs wc -l
    > 120 lib/gauntlet_rubyparser.rb
    > 1307 lib/ruby_lexer.rb
    > 1790 lib/ruby_parser.y
    > 1030 lib/ruby_parser_extras.rb
    > 4247 total


    Could you explain about what this command actually doing?

    --
    Posted via http://www.ruby-forum.com/.
     
    Thillai S., Dec 31, 2010
    #14
  15. Thillai S.

    Ryan Davis Guest

    On Dec 31, 2010, at 03:00 , Thillai S. wrote:

    > Mike Stephens wrote in post #971626:
    > In my organization we are following this standard.
    > A program should have one line comment per 2 lines of source code.
    > Kindly tell me the perl script to extract only the function code!


    PERL?!?!

    Son... them's fightin' words...
     
    Ryan Davis, Dec 31, 2010
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GIMME
    Replies:
    2
    Views:
    932
    GIMME
    Feb 11, 2004
  2. Naren
    Replies:
    0
    Views:
    612
    Naren
    May 11, 2004
  3. Christopher Diggins
    Replies:
    0
    Views:
    627
    Christopher Diggins
    Jul 9, 2007
  4. Christopher Diggins
    Replies:
    0
    Views:
    455
    Christopher Diggins
    Jul 9, 2007
  5. John Levine
    Replies:
    0
    Views:
    763
    John Levine
    Feb 2, 2012
Loading...

Share This Page