Perl program for extracting the functions alone in a Ruby file

Posted by thillaiselvan on Stack Overflow See other posts from Stack Overflow or by thillaiselvan
Published on 2011-01-08T04:03:12Z Indexed on 2011/01/08 4:53 UTC
Read the original article Hit count: 278

Filed under:

Hai all,

I am having the following Ruby program.

    puts "hai"
     def mult(a,b)
        a * b
     end
     puts "hello"
     def getCostAndMpg
       cost = 30000  # some fancy db calls go here
       mpg = 30
       return cost,mpg
     end
   AltimaCost, AltimaMpg = getCostAndMpg
   puts "AltimaCost = #{AltimaCost}, AltimaMpg = {AltimaMpg}"

I have written a perl script which will extract the functions alone in a Ruby file as follows

while (<DATA>){ 
   print if ( /def/ .. /end/ );
}

Here the <DATA> is reading from the ruby file.

So perl prograam produces the following output.

def mult(a,b)
    a * b
end
def getCostAndMpg
    cost = 30000  # some fancy db calls go here
    mpg = 30
    return cost,mpg
end

But, if the function is having block of statements, say for example it is having an if condition testing block means then it is not working. It is taking only up to the "end" of "if" block. And it is not taking up to the "end" of the function. So kindly provide solutions for me.

Example:

def function
   if x > 2
      puts "x is greater than 2"
   elsif x <= 2 and x!=0
      puts "x is 1"
  else
      puts "I can't guess the number"
  end  #----- My code parsing only up to this
end

Thanks in Advance!

© Stack Overflow or respective owner

Related posts about perl