def two_times
yield
yield
end
two_times { puts "Hello" }
# Hello
# Hello
def fib_up_to(max)
i1, i2 = 1. 1
while i1 <= max
yield i1
i1, i2 = i2, i1 + i2
end
end
fib_up_to(1000) { |f| print f, " " }
# 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
[1, 3, 5, 7, 9].each { |i| puts i }
# 1
# 3
# 5
# 7
# 9
class Array
def find
each do |value|
return value if yield(value)
end
end
end
[1,3,4,7,9].find { |v| V*V > 30 } # => 7
["H", "A", "L"].collect { |x| x.succ } # => ["I", "B", "M"]
[1,3,5,7].inject { |sum, element| sum + element } # => 16
# sum = 1, element = 3
# sum = 4, element = 5
# sum = 9, element = 7
# sum = 16
[1,3,5,6].inject { |product, element| product*element } # => 105
[1,3,5,7].inject(:+) # => 16 [1,3,5,7]/inject(:*) # => 105
f = File.open("testfile")
f.each do |line|
puts "The line is: #{line}"
end
f.close
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有