box-o-sand/RubyFun/cookbook/007/intro.rb
Dan Buch 0a9428093c Add 'RubyFun/' from commit 'b01c6826131196ba58b5288a3182f2526c89c249'
git-subtree-dir: RubyFun
git-subtree-mainline: a04a502787
git-subtree-split: b01c682613
2013-01-09 23:50:14 -05:00

40 lines
680 B
Ruby

[1, 2, 3].each { |i| puts i }
[1, 2, 3].each do |i|
if i % 2 == 0
puts "#{i} is even."
else
puts "#{i} is odd."
end
end
1.upto 3 do |x|
puts x
end
1.upto(3) { |x| puts x }
hello = lambda { "Hello" }
hello.call
log = lambda { |str| puts "[LOG] #{str}" }
log.call("A test log message.")
{1=>2, 2=>4}.each { |k,v| puts "Key #{k}, value #{v}" }
def times_n(n)
lambda { |x| x * n }
end
times_ten = times_n(10)
puts times_ten.call(5)
puts times_ten.call(1.25)
circumference = times_n(2*Math::PI)
puts circumference.call(10)
puts circumference.call(3)
puts [1, 2, 3].collect(&circumference)
ceiling = 50
puts [1, 10, 49, 50.1, 200].select { |x| x < ceiling }