diff --git a/cookbook/007/02.rb b/cookbook/007/02.rb new file mode 100644 index 0000000..19498a9 --- /dev/null +++ b/cookbook/007/02.rb @@ -0,0 +1,21 @@ +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." } + + +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 +end + +repeat(4) { puts "Hello." } + +repeat(4)