Skip to content

Instantly share code, notes, and snippets.

View 13k's full-sized avatar

K 13k

View GitHub Profile
@13k
13k / fragment_cache.rb
Created April 3, 2014 16:13
fragment cache fetch/expire
ActionController::Base.new.read_fragment('key')
ActionController::Base.new.expire_fragment('key')
FATAL -- : error adding listener addr=
/Users/k/.rbenv/versions/1.9.3-p429/lib/ruby/gems/1.9.1/gems/unicorn-4.6.2/lib/unicorn/socket_helper.rb:200:in `server_cast': undefined method `getsockname' for nil:NilClass (NoMethodError)
command Ruby19Hashes %s/\v:(\w+)(\s*)\=\>/\1:/g
@13k
13k / gist:5776800
Last active December 18, 2015 11:39
ActiveSupport::HashWithIndifferentAccess and initialization block
h = ActiveSupport::HashWithIndifferentAccess.new {|h,k| h[k] = [] }
# => {}
h[:key1]
# => []
h[:key1] << 13
# => [13]
h
# => {"key1"=>[13]}
h[:key2] << 31
# => [31]
irb(main):003:0> def assign=(val); raise "THE WORLD IS BROKEN!"; end
=> nil
irb(main):004:0> self.assign = 10
RuntimeError: THE WORLD IS BROKEN!
from (irb):3:in `assign='
from (irb):4
from C:/Program Files (x86)/ruby-1.9.2/bin/irb:12:in `<main>'
irb(main):005:0> assign = 10
=> 10
irb(main):006:0>
class C
attr_accessor :counter
def initialize
@counter = 1
end
def increment
counter = counter + 1
end
def val
13
end
def assign
val = val.to_s
end
p assign
# => ""
def f
p x
end
f()
# undefined local variable or method `x' for main:Object (NameError)
def f
if false
x = 13
@13k
13k / pry20130406-20034-1aktod6.rb
Created April 6, 2013 23:57
Hash "mapping" returning a new hash, instead of using the ugliest thing: `Hash[ Hash#map ]`
h = {a: 13, b: 31}
# => {:a=>13, :b=>31}
h.merge(h) {|k, v0, v1| v0 * 2 }
# => {:a=>26, :b=>62}
class C
attr_reader :read_only
def assert(assertion, message="assertion failed")
raise RuntimeError, message unless assertion
end
def initialize
@read_only = 13
assert read_only == 13