Parsing the Ruby File

T

Thillai S.

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,
 
T

Thillai S.

Ryan Davis wrote in post #971438:
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...
 
A

Andrew Ting

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
 
R

Ryan Davis

Ryan Davis wrote in post #971438:
=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?=
 
T

Thillai S.

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
 
R

Ryan Davis

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
 
T

Thillai S.

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

Thillai S.

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
 
R

Ryan Davis

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

Mike Stephens

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?
 
R

Ryan Davis

=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.=
 
T

Thillai S.

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!
 
T

Thillai S.

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?
 
R

Ryan Davis

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

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

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top