Skip to content

Instantly share code, notes, and snippets.

@paneq
paneq / log.txt
Created December 25, 2014 21:34
ubuntu 12.04 + ruby 2.2.0
==> p: Last 10 log lines:
==> p: compiling object_tracing.c
==> p: compiling objspace.c
==> p: linking shared-object objspace.so
==> p: make[2]: Leaving directory `/tmp/ruby-build.20141225212618.1716/ruby-2.2.0/ext/objspace'
==> p: linking shared-object date_core.so
==> p: make[2]: Leaving directory `/tmp/ruby-build.20141225212618.1716/ruby-2.2.0/ext/date'
==> p: linking shared-object nkf.so
==> p: make[2]: Leaving directory `/tmp/ruby-build.20141225212618.1716/ruby-2.2.0/ext/nkf'
==> p: make[1]: Leaving directory `/tmp/ruby-build.20141225212618.1716/ruby-2.2.0'
@paneq
paneq / dead.rb
Created December 23, 2014 14:54
dead code concern ;)
class DeadCodeConcern
def self.dead_code_or_not_dead_code?(name)
notify_honeybadger_not_dead_code(name)
notify_logger_not_dead_code(name)
end
def self.notify_honeybadger_not_dead_code(name)
Honeybadger.notify( StandardError.new("Not dead code: #{name}") )
end
evil:Inventory#change_quantity:/home/rupert/ssd/rupert/develop/paneq/inventory-mutation/lib/inventory.rb:18:a2bf6
@@ -1,8 +1,8 @@
def change_quantity(identifier, qty)
if (((qty - @reserved_quantity[identifier].sum) - @sold_quantity[identifier].sum) < 0)
- raise(StandardError, "quantity too low")
+ raise
end
@available_quantity[identifier] << (-@available_quantity[identifier].last)
@available_quantity[identifier] << qty
end
@paneq
paneq / bench.rb
Last active December 11, 2016 13:00
cost of using exceptions for control flow compared to one SQL statement (ruby 2.1.4, rails 4.1.7, sqlite) for rails-refactoring.com . Development mode executed in rails console.
require 'benchmark'
ActiveRecord::Base.logger = nil
Benchmark.bmbm do |bench|
bench.report("SQL query") do
1000.times { Whatever.count }
end
bench.report("exception hit") do
class IssuesController < ApplicationController
default_search_scope :issues
before_filter :find_issue, :only => [:show, :edit, :update]
before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy]
before_filter :find_project, :only => [:new, :create, :update_form]
before_filter :authorize, :except => [:index]
before_filter :find_optional_project, :only => [:index]
before_filter :check_for_default_issue_status, :only => [:new, :create]
before_filter :build_new_issue_from_params, :only => [:new, :create, :update_form]
@paneq
paneq / file.rb
Created July 7, 2014 15:18
slicing ruby string to bytes (because APNS) and making it still look good
"777żółć".byteslice(0,6).scrub("")
"777żółć".byteslice(0,7).scrub("")
"777żółć".byteslice(0,8).scrub("")
"777żółć".byteslice(0,9).scrub("")
@paneq
paneq / do_you_even_ruby.rb
Created December 20, 2013 15:48
Gibbon ruby-esque. not.
g = Gibbon::API.new(MAILCHIMP_API_KEY)
#=> #<Gibbon::API:0x00000007ad89d0 @api_key="KEY", @api_endpoint=nil, @timeout=nil, @throws_exceptions=nil, @default_params={:apikey=>"KEY"}>
g.templates
#=> #<Gibbon::APICategory:0x00000007100cc8 @category_name="templates", @api_key="KEY", @api_endpoint=nil, @default_params={:apikey=>"KEY"}, @throws_exceptions=true, @timeout=30>
g.respond_to?(:templates)
#=> false
g.methods.include?(:templates)
@paneq
paneq / .gitconfig
Created July 11, 2013 16:57
Avoid red color in git
[color]
ui = always
[color "diff"]
old = blue
[color "branch"]
remote = blue
plain = white
[color "status"]
added = yellow
updated = green
@paneq
paneq / bm.rb
Created May 14, 2013 09:36
Struct vs attr_accessor
require 'benchmark'
s1 = Struct.new(:login, :password).new("login", "password")
class S
attr_accessor :login, :password
def initialize(login, password)
@login = login
@password = password
end
@paneq
paneq / page.conf
Created April 3, 2013 14:57
nginx maintenance page that is JSON friendly
if (-f $document_root/system/maintenance.html) {
return 503;
}
error_page 503 @maintenance;
location @maintenance {
internal;
if ($http_accept ~ json) {
return 503 "{}";
}