From 697fc60bcbf69ca8665ba01a31e4bcd5e8481a1e Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Thu, 6 Oct 2011 22:17:11 -0400 Subject: [PATCH] working through discussion of 7.2 --- cookbook/007/02.rb | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/cookbook/007/02.rb b/cookbook/007/02.rb index 19498a9..05b4f95 100644 --- a/cookbook/007/02.rb +++ b/cookbook/007/02.rb @@ -1,21 +1,14 @@ def call_twice - puts "I'm about to call your block." - yield - puts "I'm about to call your block again." - yield -end - -call_twice { puts "Hi, I'm a talking code block." } - + puts "Calling your block." + ret1 = yield("very first") + puts "The value of your block: #{ret1}" -def repeat(n) - if block_given? - n.times { yield } - else - raise ArgumentError.new("I can't repeat a block you don't give me!") - end + puts "Calling your block again." + ret2 = yield("second") + puts "The value of your block: #{ret2}" end -repeat(4) { puts "Hello." } - -repeat(4) +call_twice do |which_time| + puts "I'm a code block, called for the #{which_time} time." + which_time == "very first" ? 1 : 2 +end