From de8cf132895ed61b2e88998c2b81e8740de8c80a Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Thu, 6 Oct 2011 21:56:49 -0400 Subject: [PATCH] more intro stuff for ch 7 --- cookbook/007/intro.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/cookbook/007/intro.rb b/cookbook/007/intro.rb index 6e32b06..e0efa46 100644 --- a/cookbook/007/intro.rb +++ b/cookbook/007/intro.rb @@ -13,3 +13,27 @@ end 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 }