【发布时间】:2011-01-08 17:34:15
【问题描述】:
我有以下 Ruby 程序。
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}"
我编写了一个 perl 脚本,它将单独提取 Ruby 文件中的函数,如下所示
while (<DATA>){
print if ( /def/ .. /end/ );
}
这里的<DATA> 正在从 ruby 文件中读取。
所以 perl 程序产生以下输出。
def mult(a,b)
a * b
end
def getCostAndMpg
cost = 30000 # some fancy db calls go here
mpg = 30
return cost,mpg
end
但是,如果函数有语句块,例如它有一个 if 条件测试块,则意味着它不工作。它只占用“if”块的“end”。而且它没有占用功能的“结束”。请为我提供解决方案。
例子:
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
提前致谢!
【问题讨论】:
标签: perl