Skip to content

Instantly share code, notes, and snippets.

Avatar

Mike Perham mperham

View GitHub Profile
@mperham
mperham / gist:8922782
Created Feb 10, 2014
Rubby numeric handling
View gist:8922782
> require 'bigdecimal'
=> true
> Float('foo')
ArgumentError: invalid value for Float(): "foo"
from (irb):2:in `Float'
from (irb):2
from /Users/mikep/.rubies/ruby-2.0.0-p353/bin/irb:12:in `<main>'
> Integer('foo')
ArgumentError: invalid value for Integer(): "foo"
from (irb):3:in `Integer'
@mperham
mperham / gist:9864177
Created Mar 29, 2014
mime-types - such garbage
View gist:9864177
640 ::CLASS
645 /Users/mikep/.rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:ARRAY
676 /Users/mikep/.rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/stub_specification.rb:21:STRING
722 /Users/mikep/.rubies/ruby-2.1.1/lib/ruby/2.1.0/x86_64-darwin13.0/openssl.bundle:0:STRING
724 /Users/mikep/.rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/specification.rb:1338:OBJECT
734 /Users/mikep/.rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/requirement.rb:92:ARRAY
734 /Users/mikep/.rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/requirement.rb:92:STRING
872 /Users/mikep/.rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/requirement.rb:53:OBJECT
876 /Users/mikep/.gem/ruby/2.1.1/gems/sprockets-2.10.0/lib/sprockets/mime.rb:1:STRING
884 /Users/mikep/.gem/ruby/2.1.1/gems/mime-types-1.25/lib/mime/types.rb:423:STRING
@mperham
mperham / bench.rb
Last active Aug 29, 2015
Sidekiq performance 2014
View bench.rb
require 'sidekiq'
class LazyWorker
include Sidekiq::Worker
def perform
# nothing
end
end
Sidekiq.configure_client do |config|
View gist:11262832
Allowing ISPs to charge for different levels of access to consumers
creates an uneven playing field for Internet-based companies. As a
consumer, I already pay an ISP monthly to provide me a level of service when
accessing the internet. I expect that access to be fair for all
companies so that I have equal access to all services on the Internet.
Allowing large companies to pay for faster "lanes" means that small
companies cannot compete fairly and hurts the normal pace of
innovation. Please keep Net Neutrality in place so the Internet
continues to be a place of innovation and not monied incumbents.
@mperham
mperham / gc opts
Last active Aug 29, 2015
Ruby 2.1.2 memory bloat/leak
View gc opts
> pp ENV.select {|x| x =~ /^RUBY/ }
{"RUBY_GC_HEAP_GROWTH_FACTOR"=>"1.3",
"RUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR"=>"1.3",
"RUBY_GC_HEAP_INIT_SLOTS"=>"600000",
"RUBY_GC_MALLOC_LIMIT"=>"90000000",
"RUBYOPT"=>"-rbundler/setup",
"RUBYLIB"=>
"/usr/local/rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib"}
View gist:7d763bdc42465caf17c7

I want to build a system which uses rules. These rules will change hourly (i.e. "this rule is effective from 2pm to 6pm"). The rules must support arbitrarily complex logic on just 2-3 pre-defined variables and result in a boolean:

item.a > 10 && item.b == 0 || item.category == FOOTWEAR

These rules will be executed hundreds of times per second so I can't afford the overhead of plain old eval. I want to reload the current effective ruleset from the database hourly, precompile each rule's logic string and execute it via the quickest method possible. It might look something like this:

class Rule
@mperham
mperham / repro.md
Created Jul 22, 2015
Sidekiq / Rails 5 interlockup
View repro.md
git clone git://github.com/mperham/sidekiq
cd sidekiq && git checkout rails5
cd myapp && bundle
bundle exec sidekiq
# in another terminal tab
bundle exec rails s

Now hit http://localhost:3000/work to create a random set of jobs for sidekiq to perform.

View gist:23693
diff --git a/lib/starling/handler.rb b/lib/starling/handler.rb
index 4ab2960..1bde8ac 100644
--- a/lib/starling/handler.rb
+++ b/lib/starling/handler.rb
@@ -56,6 +56,7 @@ STAT queue_%s_age %d\r\n".freeze
SHUTDOWN_COMMAND = /\Ashutdown\r\n/m
+ QUIT_COMMAND = /\Aquit\r\n/m
View gist:24252
diff --git a/vendor/plugins/geokit/lib/geo_kit/acts_as_mappable.rb b/vendor/plugins/geokit/lib/geo_kit/acts_as_mappable.rb
index c6c5eba..23dbab1 100644
--- a/vendor/plugins/geokit/lib/geo_kit/acts_as_mappable.rb
+++ b/vendor/plugins/geokit/lib/geo_kit/acts_as_mappable.rb
@@ -377,20 +377,22 @@ module GeoKit
lat = deg2rad(origin.lat)
lng = deg2rad(origin.lng)
multiplier = units_sphere_multiplier(units)
- case connection.adapter_name.downcase
- when "sqlite"
View gist:72917
require 'rubygems'
gem 'rails', '2.2.2'
gem 'json', '1.1.3' # or json_pure, both break
# Switch these requires and it works
require 'json'
require 'activesupport'
data = { :a => [1,2,3] }
puts data.to_json