class Object
Public Instance Methods
log(s = nil)
click to toggle source
This file contains global helper methods
# File lib/pennyworth/helper.rb, line 20 def log s = nil puts s unless Pennyworth::Cli.settings.silent end
measure(label, &block)
click to toggle source
# File lib/pennyworth/spec_profiler.rb, line 20 def measure(label, &block) own_parent = @parent_measurement measurement = { label: label, child_measurements: [] } @parent_measurement = measurement time = Benchmark.measure do block.call end @parent_measurement = own_parent measurement.merge!(time: time.real) # Calculate time that was not spent in the measured child blocks but somewhere # else if !measurement[:child_measurements].empty? other_time = measurement[:time] measurement[:child_measurements].each do |child| other_time -= child[:time] end measurement[:child_measurements] << { label: "Other", time: other_time } end if own_parent own_parent[:child_measurements] << measurement else @measurements << measurement end end
print_measurement(measurement, indent = 0)
click to toggle source
# File lib/pennyworth/spec_profiler.rb, line 51 def print_measurement(measurement, indent = 0) name = measurement[:label][0..65-indent].ljust(70-indent) STDERR.puts (" " * indent) + "#{name}: #{measurement[:time]}" if measurement[:child_measurements] measurement[:child_measurements].each do |child| print_measurement(child, indent + 2) end end end
with_c_locale(&block)
click to toggle source
# File lib/pennyworth/helper.rb, line 24 def with_c_locale(&block) with_env "LC_ALL" => "C", &block end
with_env(env) { || ... }
click to toggle source
# File lib/pennyworth/helper.rb, line 28 def with_env(env) # ENV isn't a Hash, but a weird Hash-like object. Calling #to_hash on it # will copy its items into a newly created Hash instance. This approach # ensures that any modifications of ENV won't affect the stored value. saved_env = ENV.to_hash begin ENV.replace(saved_env.merge(env)) yield ensure ENV.replace(saved_env) end end