Skip to content

Instantly share code, notes, and snippets.

View mikz's full-sized avatar

Michal Cichra mikz

View GitHub Profile
array = 1_000_000.times.map{ rand(0..3) }
Benchmark.bm do |x|
x.report('check') { array.each_slice(2){|a,b| b == 0 ? 0 : a / b } }
x.report('rescue') { array.each_slice(2){|a,b| a / b rescue 0 } }
end
@mikz
mikz / script_pry.rb
Created July 4, 2012 09:48
script/pry for Rails 2.3
#!/usr/bin/env ruby
console = File.expand_path(File.dirname(__FILE__), '.') + "/console"
exec console, *ARGV, '--irb=pry'
@mikz
mikz / october_daemon.rb
Created June 29, 2012 16:43
October Daemon
module October
class Daemon
attr_reader :child, :root, :env
def initialize(root, env = nil)
@root = root
@env = env
end
def spawn!
@mikz
mikz / gist:2813751
Created May 27, 2012 12:27
Make Rails 2.3.x play nice with Ruby 1.9.x. Forces encoding to UTF8 for every string in parameters.
module UTF8
module Params
private
def normalize_parameters_with_encoding(value)
normalize_parameters_without_encoding(value).tap do |value|
value.force_encoding(Encoding.default_external) if value.respond_to?(:force_encoding)
end
end
end
end
class Example
attr_reader :value
def initialize(value)
@value = value
end
def truthy?
!!@value
end
@mikz
mikz / unicorn.rb
Created April 2, 2012 13:59
unicorn config
preload_app true
worker_processes 2
listen 3000
before_fork do |server, worker|
ActiveRecord::Base.connection.disconnect!
end
after_fork do |server, worker|
NoMethodError:
undefined method `split' for ["blob"]:Array
# /Users/mikz/.rvm/gems/ruby-1.9.3-p125-falcon@thesis/gems/rspec-expectations-2.9.0/lib/rspec/expectations/differ.rb:13:in `diff_as_string'
# /Users/mikz/.rvm/gems/ruby-1.9.3-p125-falcon@thesis/gems/rspec-expectations-2.9.0/lib/rspec/expectations/fail_with.rb:25:in `fail_with'
# /Users/mikz/.rvm/gems/ruby-1.9.3-p125-falcon@thesis/gems/rspec-expectations-2.9.0/lib/rspec/expectations/handler.rb:17:in `handle_matcher'
# /Users/mikz/.rvm/gems/ruby-1.9.3-p125-falcon@thesis/gems/rspec-expectations-2.9.0/lib/rspec/expectations/extensions/kernel.rb:12:in `should'
# /Users/mikz/.rvm/gems/ruby-1.9.3-p125-falcon@thesis/gems/rspec-core-2.9.0/lib/rspec/core/subject.rb:54:in `should'
# ./spec/test_spec.rb:5:in `block (2 levels) in <top (required)>'
var d = new Date()
var h = d.getHours()
if (h < 5){
document.write("Today is ")
}
else
if (h < 9){
document.write("Today is ")
}
diff --git a/test/test_helpers/redis.rb b/test/test_helpers/redis.rb
index 3ba8649..fd0fa1e 100644
--- a/test/test_helpers/redis.rb
+++ b/test/test_helpers/redis.rb
@@ -1,7 +1,13 @@
+require 'backend/storage'
+
module TestHelpers
module Redis
private
def instance_method_already_implemented?(method_name)
method_name = method_name.to_s
return true if method_name =~ /^id(=$|\?$|$)/
@_defined_class_methods ||= ancestors.first(ancestors.index(ActiveRecord::Base)).sum([]) { |m| m.public_instance_methods(false) | m.private_instance_methods(false) | m.protected_instance_methods(false) }.map(&:to_s).to_set
@@_defined_activerecord_methods ||= (ActiveRecord::Base.public_instance_methods(false) | ActiveRecord::Base.private_instance_methods(false) | ActiveRecord::Base.protected_instance_methods(false)).map(&:to_s).to_set
raise DangerousAttributeError, "#{method_name} is defined by ActiveRecord" if @@_defined_activerecord_methods.include?(method_name)
@_defined_class_methods.include?(method_name)
end