working through discussion of 7.2

cat-town
Dan Buch 13 years ago
parent 2194c6637e
commit 697fc60bcb

@ -1,21 +1,14 @@
def call_twice def call_twice
puts "I'm about to call your block." puts "Calling your block."
yield ret1 = yield("very first")
puts "I'm about to call your block again." puts "The value of your block: #{ret1}"
yield
end
call_twice { puts "Hi, I'm a talking code block." }
def repeat(n) puts "Calling your block again."
if block_given? ret2 = yield("second")
n.times { yield } puts "The value of your block: #{ret2}"
else
raise ArgumentError.new("I can't repeat a block you don't give me!")
end
end end
repeat(4) { puts "Hello." } call_twice do |which_time|
puts "I'm a code block, called for the #{which_time} time."
repeat(4) which_time == "very first" ? 1 : 2
end

Loading…
Cancel
Save