doing 7.2
This commit is contained in:
parent
584e1eceb2
commit
2194c6637e
21
cookbook/007/02.rb
Normal file
21
cookbook/007/02.rb
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user