Topic: Blocks
def grandfatherClock &block
currentTime = Time.now.strftime("%I")
currentTimeI = currentTime.to_i
total = ((1)..(currentTimeI)).to_a
total.each do |t|
puts 'The time now is ' + t.to_s + ' O\' Clock'
block.call
end
endgrandfatherClock do
puts 'dong'
endgrandfatherClock do
puts 'ding'
end
I've coded the click something like this to achieve:
Grandfather Clock. Write a method which takes a block and calls it once for each hour that has passed today. That way, if I were to pass in the block do puts 'DONG!' end, it would chime (sort of) like a grandfather clock. Test your method out with a few different blocks (including the one I just gave you). Hint: You can use Time.now.hour to get the current hour. However, this returns a number between 0 and 23, so you will have to alter those numbers in order to get ordinary clock-face numbers (1 to 12).
rtices
What would you do? I'm a beginner and wants to learn best practices. ![]()
Last edited by Haris (2007-08-02 19:19:34)