#定义一个空数组
arr=[]
#从2到10循环
2.upto(10) do |i|
#这个临时数组用于存放数i与素数表的余数
result=[]
arr.collect do |j|
result.push i%j
end
#如果不能被素数整除,说明是素数,加入到素数表
if !result.include?(0)
arr.push(i)
end
end
puts arr
#素数表
#puts (2..10).inject([]){|a, e| (a.map {|x| e % x}).include?(0) ? a : a<<e}
#定义一个过程
def Fibonacci(ar)
#从0开始循环到数组结束
0.upto(ar.length) do |index|
if index<2
ar[index]=1
else
ar[index]=ar[index-1]+ar[index-2]
end
end
return ar
end
puts Fibonacci([0]*10)
#斐波纳契数列
#puts lambda {|ar| ar.each_index {|i| ar[i]=i<2? 1 : ar[i-2]+ar[i-1]}}.call([0]*10)
actually ruby is fair precise...at least much more precise than c# (maybe also java). there are so many "exceptional cases" in c#, so many violation of syntax rules by itself. (for an instance, who can tell me why i need a "delegate" to pass/return a obvious function?) on the other hand, ruby has a fair small set of syntax rules (although not as small as smalltalk or lisp). all these fancy usage derive from and comply with a few precise rules. from my point of view a really precise language should look like that.