Created
March 17, 2018 22:01
-
-
Save veronicacannon/f75ab27c44d81139a4b9ec0c30ff7717 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://augustl.com/blog/2008/procs_blocks_and_anonymous_functions/ | |
Procs are useful code organization tools. Let’s say that you want to calculate the running time for a few code snippets, in order to benchmark them. To do this elegantly, you want to be able to call a method and pass some code to it, and have the execution time returned. | |
Here’s how you can use procs to do just that: | |
def time(a_proc) | |
start = Time.now | |
a_proc.call | |
puts Time.now - start | |
end | |
time(Proc.new { code_here }) | |
time(Proc.new { more_code_here }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment