working through discussion of 7.2
This commit is contained in:
parent
2194c6637e
commit
697fc60bcb
@ -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
|
||||
puts "Calling your block."
|
||||
ret1 = yield("very first")
|
||||
puts "The value of your block: #{ret1}"
|
||||
|
||||
puts "Calling your block again."
|
||||
ret2 = yield("second")
|
||||
puts "The value of your block: #{ret2}"
|
||||
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!")
|
||||
call_twice do |which_time|
|
||||
puts "I'm a code block, called for the #{which_time} time."
|
||||
which_time == "very first" ? 1 : 2
|
||||
end
|
||||
end
|
||||
|
||||
repeat(4) { puts "Hello." }
|
||||
|
||||
repeat(4)
|
||||
|
Loading…
Reference in New Issue
Block a user