Parsing the Ruby File

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/\.

What are you actually trying to do?

···

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

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

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/.

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/.

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/\.

Ryan Davis wrote in post #971438:

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

···

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

#################################################################################
# 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/\.

If there are 40 lines of code in the function, then it should have
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 => '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

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 => '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

But it's the same exact code!

Now let's look at your actual comment block:

···

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

#
# 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 => 'Mon' , # EXACT DUPE OF CODE
# 2 => 'Tues' ,
# 3 => 'Wed' ,
# 4 => 'Thu' ,
# 5 => 'Fri' ,
# 6 => 'Sat' ,
# 7 => 'Sun' ,
# 8 => 'Leave' ,
# 9 => '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

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

Boy do I have a text editor to sell to you! You'll LOVE what it'll do to "improve" your code quality.

···

On Dec 31, 2010, at 2:48, Mike Stephens <rubfor@recitel.net> wrote:

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.

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/.

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/\.

That didn't answer my question, it merely repeated your original problem statement.

Why do you want that? What are you trying to achieve?

···

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

Ryan Davis wrote in post #971438:

On Dec 30, 2010, at 2:56, "Thillai S." <thillaiselvan@gmail.com> 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.

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/\.

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/\.

PERL?!?!

Son... them's fightin' words...

···

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!

kindly read my response.

···

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.