Skip to content

Instantly share code, notes, and snippets.

class PublishesPost
def self.call(*args)
new(*args).call
end
attr_reader :post, :user
def initialize(post, user)
@post, @user = post, user
@iain
iain / gist:2563017
Created April 30, 2012 21:58
global variables in Ruby and the 'english' extension
>> global_variables
=> [:$;, :$-F, :$@, :$!, :$SAFE, :$~, :$&, :$`, :$', :$+, :$=, :$KCODE, :$-K, :$,, :$/, :$-0,
:$\, :$_, :$stdin, :$stdout, :$stderr, :$>, :$<, :$., :$FILENAME, :$-i, :$*, :$?, :$$, :$:,
:$-I, :$LOAD_PATH, :$", :$LOADED_FEATURES, :$VERBOSE, :$-v, :$-w, :$-W, :$DEBUG, :$-d, :$0,
:$PROGRAM_NAME, :$-p, :$-l, :$-a, :$binding, :$1, :$2, :$3, :$4, :$5, :$6, :$7, :$8, :$9]
>> require 'english'
=> true
>> global_variables
@iain
iain / sqlserver.rb
Created March 9, 2012 11:46
Monkey Patch of Today
if defined?(ActiveRecord::ConnectionAdapters::SQLServerAdapter)
ActiveRecord::ConnectionAdapters::SQLServerAdapter.enable_default_unicode_types = true
ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_text_database_type = 'ntext'
ActiveRecord::ConnectionAdapters::SQLServerAdapter.native_string_database_type = 'nvarchar'
end
version = ActiveRecord::ConnectionAdapters::Sqlserver::Version::VERSION
preferred_version = '3.2.1'
if version == preferred_version
# spec/support/soap_client.rb
class SoapClient
def self.call(env)
new(env).response
end
def initialize(env)
@env = env
@request = Rack::Request.new(env)
@iain
iain / gist:1722900
Created February 2, 2012 11:01
Kill me now!
def some_method(param)
foo = nil
if param
if param == "value"
foo = get_foo_somehow
else
foo = get_foo_some_other_way
end
unless foo
foo = find_foo
class Users::Register
def self.call(attributes)
new(attributes).call
end
def initialize(attributes)
@attributes = attributes
end
@iain
iain / environment.rb
Created January 9, 2012 18:48
Logging Rails Loading Times
# Start tracking how much time you lose by loading Rails constantly
# by adding a couple of lines to config/environment.rb
# The first one is on top, the rest at the bottom of the file...
start_time = Time.now
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
YOUR_APPLICATION_NAME::Application.initialize!
occurrence = stub
def occurrence.update_attributes(attrs)
attrs.each do |key, value|
stub(key).and_return(value)
end
end
syntax on
filetype plugin indent on
" Don't redraw during macro execution
set lazyredraw
" tir_black looks most like ir_black in console
colorscheme tir_black
" Syntax coloring lines that are too long just slows down the world
@iain
iain / gist:1574837
Created January 7, 2012 14:12
RSpec 2.7 vs 2.8

RSpec 2.7:

Finished in 37.42 seconds
306 examples, 0 failures, 2 pending
rspec  22.39s user 16.56s system 102% cpu 37.925 total

RSpec 2.8: