Skip to content

Instantly share code, notes, and snippets.

@eac
eac / gist:5011b6c53578c1d20773
Created May 23, 2014 19:48
method check benchmark
require 'active_support/all'
def time
cpu = nil
real = Benchmark.ms do
cpu = Process.clock_gettime(Process::CLOCK_PROCESS_CPUTIME_ID, :millisecond)
yield
cpu = Process.clock_gettime(Process::CLOCK_PROCESS_CPUTIME_ID, :millisecond) - cpu
end
{ real: real, cpu: cpu, idle: real-cpu }
@eac
eac / gist:3020132
Created June 29, 2012 19:31
Force encoding client input
def force_utf8_encoding!(str)
str.force_encoding(Encoding::UTF_8)
if !str.valid_encoding?
logger.warn("encoding: forcing invalid UTF-8 string")
logger.warn("encoding: text is #{str}")
str.encode!(Encoding::UTF_8, Encoding::ISO_8859_1)
end
end