Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Parsing the Ruby File

Reply
Thread Tools

Parsing the Ruby File

 
 
Thillai S.
Guest
Posts: n/a
 
      12-30-2010
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/.

 
Reply With Quote
 
 
 
 
Ryan Davis
Guest
Posts: n/a
 
      12-30-2010


On Dec 30, 2010, at 2:56, "Thillai S." <(E-Mail Removed)> wrote:

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


What are you actually trying to do?

 
Reply With Quote
 
 
 
 
Thillai S.
Guest
Posts: n/a
 
      12-31-2010
Ryan Davis wrote in post #971438:
> On Dec 30, 2010, at 2:56, "Thillai S." <(E-Mail Removed)> 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/.

 
Reply With Quote
 
Andrew Ting
Guest
Posts: n/a
 
      12-31-2010
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/.

 
Reply With Quote
 
Ryan Davis
Guest
Posts: n/a
 
      12-31-2010


On Dec 30, 2010, at 20:40, "Thillai S." <(E-Mail Removed)> wrote:

> Ryan Davis wrote in post #971438:
>> On Dec 30, 2010, at 2:56, "Thillai S." <(E-Mail Removed)> 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?=

 
Reply With Quote
 
Thillai S.
Guest
Posts: n/a
 
      12-31-2010
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/.

 
Reply With Quote
 
Ryan Davis
Guest
Posts: n/a
 
      12-31-2010

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



 
Reply With Quote
 
Thillai S.
Guest
Posts: n/a
 
      12-31-2010
> 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/.

 
Reply With Quote
 
Thillai S.
Guest
Posts: n/a
 
      12-31-2010
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/.

 
Reply With Quote
 
Ryan Davis
Guest
Posts: n/a
 
      12-31-2010

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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[ANN] Parsing Tutorial and YARD 1.0: A C++ Parsing Framework Christopher Diggins C++ 0 07-09-2007 09:01 PM
[ANN] Parsing Tutorial and YARD 1.0: A C++ Parsing Framework Christopher Diggins C++ 0 07-09-2007 08:58 PM
In file parsing, taking the first few characters of a text file after a readfile or streamreader file read... .Net Sports ASP .Net 11 01-17-2006 12:44 AM
SAX Parsing - Weird results when parsing content between tags. Naren XML 0 05-11-2004 07:25 PM
Perl expression for parsing CSV (ignoring parsing commas when in double quotes) GIMME Perl 2 02-11-2004 05:40 PM



Advertisments