Complete the code to require the Ruby profiler library.
require '[1]'
The Ruby standard library for profiling code is called profile. You need to require it to use profiling features.
Complete the code to start profiling a block of code.
[1] do sleep(1) end
Profiler instead of Profile causes an error..profile or using lowercase module name causes errors.The Profile.profile method is used with a block to profile the code inside. It must be capitalized Profile and lowercase profile method, used with do to start the block. This convenience method handles start, run block, stop, and print_profile automatically.
Fix the error in the code to correctly profile a method execution.
require 'profile' def slow_method sleep(2) end [1]
Profile.profile slow_method without a block causes an error.} or end causes syntax errors.The Profile.profile method expects a block. You must pass the method call inside a block using curly braces { slow_method } or do...end.
Fill both blanks to create a hash that stores method names and their execution times.
times = { [1] => 1.2, [2] => 0.5 }We use symbols as keys to represent method names. Here, :slow_method and :fast_method are used as keys with their execution times as values.
Fill all three blanks to filter and print methods with execution time greater than 1 second.
times.select { |[1], [2]| [2] [3] 1 }.each do |method, time|
puts "Method: #{method}, Time: #{time}s"
endThe block variables are method and time. We filter with time > 1 to get methods taking more than 1 second.