Skip to content

Instantly share code, notes, and snippets.

View danhodge's full-sized avatar

Dan Hodge danhodge

View GitHub Profile
@danhodge
danhodge / arrays.rb
Created October 2, 2011 20:19
Ruby Array Tips
# Use .transpose to 'unzip' an array of arrays
[ [ 1, 'a' ], [ 2, 'b' ], [ 3, 'c' ] ].transpose # => [ [ 1, 2, 3 ], [ 'a', 'b', 'c' ] ]
@danhodge
danhodge / formatting.rb
Created October 2, 2011 20:23
Ruby Formatting Tips
# Print a 32-bit integer as an 8 character hex string
"%08X" % 65535 # => "0000FFFF"
@danhodge
danhodge / attrs_18_vs_19.rb
Created October 3, 2011 19:29
attr_* methods invoked from private methods behave differently in REE 1.8.7 and MRI 1.9.2
require 'rubygems'
require 'active_support'
module Attrs
extend ActiveSupport::Concern
module ClassMethods
def add_public_accessor(name)
add_accessor(name)
end
@danhodge
danhodge / param_filters.rb
Created February 25, 2012 02:51
Rails Parameter Filters
# run from the console of your Rails project
param_filter = ActionDispatch::Http::ParameterFilter.new([:foo, :bar])
param_filter.filter("foo" => "hi") # => {"foo"=>"[FILTERED]"}
param_filter.filter("BAR" => "123") # => {"BAR"=>"[FILTERED]"}
param_filter.filter("Food" => "nachos") # => {"Food"=>"[FILTERED]"}
param_filter.filter("hobart" => "australia") # => {"hobart"=>"[FILTERED]"}
@danhodge
danhodge / hello_spec.rb
Created March 1, 2012 16:58
achievement unlocked!
describe MessageReceiver do
context 'with a hello message' do
let(:message) { "hello" }
it 'responds accordingly' do
subject.receive(message).should == 'is it me your looking for?'
end
end
end
@danhodge
danhodge / gist:4177114
Last active October 13, 2015 09:47 — forked from lucasfais/gist:1207002
Sublime Text 2 - Useful Shortcuts

Sublime Text 2 – Useful Shortcuts (Mac OS X)

General

⌘T go to file
⌘⌃P go to project
⌘R go to methods
⌃G go to line
⌘KB toggle side bar
⌘⇧P command prompt
class Monad
def self.unit(value)
monad = new
monad.define_singleton_method(:bind) { |func, *args| func.call(*([value] + args)) }
monad
end
def lift(name, func, *args)
define_singleton_method(name) { Monad.unit(bind(func, args)) }
"J\u00e4germeister" # => "Jägermeister"
"Caf\u00e9" # => "Café"
@danhodge
danhodge / class_delegators.rb
Last active August 29, 2015 13:57
Ruby Delegators
require 'forwardable'
class Foo
extend Forwardable
# Note: def_delegators, not def_delegator
def_delegators :ar, :size, :map
def ar
[1, 2, 3]
end
@danhodge
danhodge / dates.rb
Created August 4, 2014 14:23
Common date ranges using Active Support's date calculations
# require 'active_support'
# Monday, August 3, 2014
today = Date.today #=> Mon, 04 Aug 2014
Date.yesterday #=> Sun, 03 Aug 2014
Date.tomorrow #=> Tue, 05 Aug 2014
# this week
today.beginning_of_week #=> Mon, 04 Aug 2014