#### 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    endendgrandfatherClock 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)

#### Re: Blocks

You're almost there. You just need to implement the hint. Figure out the pattern you need to convert a 24 hour clock to a 12 hour clock and you'll have almost finished the method.

What math would you use to convert the number in the left column to the corresponding number in the right column?

Time.now.hour,    # times to chime
0  1
1  2
2  3
3  4
4  5
5  6
6  7
7  8
9  10
10  11
11  12
-------
12  1
13  2
14  3
15  4
16  5
17  6
18  7
19  8
20  9
21  10
22  11
23  12

---------------
EDIT: I see that you've gotten past that part already. If you want to continue using strftime():

`Time.now.strftime("%I").to_i.times do  block.callend`

Last edited by vwoo (2007-08-02 20:00:20)

Vincent Woo Ruby on Rails Blog

#### Re: Blocks

`def blck string, &block    puts 'Block ' + string + ' starts..'    puts 'Rendered:' + block.call.to_s    puts 'Block ' + string + ' ends..'endblck '31' do    puts 'Hi'end`

How can I make block.call not to become nil when called as a string?

#### Re: Blocks

`def blck string, &block  puts 'Block ' + string + ' starts..'  puts 'Rendered:' + block.call  puts 'Block ' + string + ' ends..'endblck '31' do  # The last value here is returned to blck()  'Hi'end`

Vincent Woo Ruby on Rails Blog

#### Re: Blocks

vwoo wrote:

`def blck string, &block  puts 'Block ' + string + ' starts..'  puts 'Rendered:' + block.call  puts 'Block ' + string + ' ends..'endblck '31' do  # The last value here is returned to blck()  'Hi'end`

How would I go about doing a block into a block?

`def blck string, &block puts 'Block ' + string + ' starts..' puts 'Rendered:' + block.call puts 'Block ' + string + ' ends..' if def sblck sstring, &sblock puts 'Small Block ' + sstring + ' starts..' puts 'Rendered:' + sblock.call puts 'Small Block ' + sstring + ' ends..' endendblck '31' do   'Hi'sblck 'mulitply' do    number = 10    (number * 5).to_sendend`

#### Re: Blocks

sblck() is a repeat of blck() so it isn't necessary. You could call blck() as many times as you need to.

`def blck string, &block  puts 'Block ' + string + ' starts..'  puts 'Rendered:' + block.call  puts 'Block ' + string + ' ends..'endblck '31' do  'Hi'end# Output:# Block 31 starts..# Rendered:Hi# Block 31 ends..blck 'multiply' do  number = 10  (number * 5).to_send# Output:# Block multiply starts..# Rendered:50# Block multiply ends..`

But you could call a method from any other method:
`def print_once &blk  p blk.callenddef print_twice &blk  message = blk.call  print_once {message}  # A way to define short code blocks is with {}.  print_once {message}endprint_once {"This is a message"}# "This is a message"print_twice {"ditto"}# "ditto"# "ditto"`

Last edited by vwoo (2007-08-03 15:17:47)

Vincent Woo Ruby on Rails Blog

#### Re: Blocks

Here's my solution to the grandfather clock problem:

https://gist.github.com/2760616