Skip to content

Instantly share code, notes, and snippets.

@veronicacannon
Created March 17, 2018 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veronicacannon/f75ab27c44d81139a4b9ec0c30ff7717 to your computer and use it in GitHub Desktop.
Save veronicacannon/f75ab27c44d81139a4b9ec0c30ff7717 to your computer and use it in GitHub Desktop.
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