A Few Things to Try

  • Even better profiling. After you do your profiling, see the slow parts of your program, and either make them faster or learn to love them as they are, you probably don’t want to see all of that profiling anymore. But (I hope) you’re too lazy to go back and delete it all…especially because you might want to use it again someday. Modify the profile method so you can turn all profiling on and off by changing just one line of code. Just one word!

  • Grandfather clock. Write a method that 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.

    Hint: You can use Time.new.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).

  • Program logger. Write a method called log that takes a string description of a block (and, of course, a block). Similar to the method do_self_importantly, it should puts a string telling you it started the block and another string at the end telling you it finished and also telling you what the block returned. Test your method by sending it a code block. Inside the block, put another call to log, passing a block to it. In other words, your output should look something like this:

    Beginning "outer block"...
    Beginning "some little block"...
    ..."some little block" finished, returning:
    5
    Beginning "yet another block"...
    ..."yet another block" finished, returning:
    I like Thai food!
    ..."outer block" finished, returning: false
  • Better program logger. The output from that last logger was kind of hard to read, and it would just get worse the more you used it. It would be so much easier to read if it indented the lines in the inner blocks. So, you’ll need to keep track of how deeply nested you are every time the logger wants to write something. To do this, use a global variable, which is a variable you can see from anywhere in your code. To make a global variable, just precede your variable name with $, like so: $global, $nesting_depth, and $big_top_pee_wee. In the end, your logger should output code like this:

    Beginning "outer block"...
    Beginning "some little block"...
    Beginning "teeny-tiny block"...
    ..."teeny-tiny block" finished, returning:
    lots of love
    ..."some little block" finished, returning:
    42
    Beginning "yet another block"...
    ..."yet another block" finished, returning:
    I love Indian food!
    ..."outer block" finished, returning:
    true
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.8.212