Skip to content

Instantly share code, notes, and snippets.

@shime
shime / application_controller.rb
Created October 28, 2012 15:19
gist describing using rescue_from in a Rails app that uses Exceptional
class ApplicationController < ActionController::Base
protect_from_forgery
rescue_from Exception, with: lambda { |exception| render_error 500, exception }
rescue_from ActionController::RoutingError, ActionController::UnknownController, ::AbstractController::ActionNotFound, ActiveRecord::RecordNotFound, with: lambda { |exception| render_error 404, exception }
def render_error(status, exception)
render template: "errors/error_#{status}", layout: 'layouts/application', status: status
end
end
@shime
shime / log.txt
Created August 19, 2012 11:32 — forked from steveklabnik/log.txt
A fun shell script from #euruku
$ history | awk {'print $2, $3, $4'} | sort | uniq -c | sort -k1 -rn | head -n 30
610 git status
568 git commit -m
491 git add .
252 git push origin
176 bundle
138 rails s
128 ls
120 git commit --amend
114 git reset --hard
@shime
shime / cooks.rb
Created August 7, 2012 13:33
overriding instance method with module, solution by Markus
module CanCook
def bake(food_name)
"I'm baking the #{food_name}!"
end
end
class Programmer
def initialize
self.extend CanCook
end
@shime
shime / cooks.rb
Created August 7, 2012 13:25
overriding instance method with module, by @ryanlecompte
module Prepending
def append_features(base)
prepend = self
base.define_singleton_method(:new) do |*args, &block|
super(*args, &block).extend(prepend)
end
end
end
module CanCook
@shime
shime / cooks.rb
Created August 6, 2012 12:21
overriding instance method with module, ultimate solution
module Prepending
def append_features(base)
prepend = self
base.extend Module.new { define_method(:new) { |*args,&block| super(*args,&block).extend(prepend) }}
end
end
module CanCook
extend Prepending
@shime
shime / cooks.rb
Created August 6, 2012 12:09
overriding instance method with module, fifth solution
require "prepend"
module CanCook
def bake(food_name)
"I'm baking the #{food_name}!"
end
end
class Programmer
def bake(food_name)
"I don't know how to bake #{food_name} :("
@shime
shime / cooks.rb
Created August 6, 2012 11:59
overriding instance method with module, alternative
module CanCook
def override_bake(food_name)
"I'm baking the #{food_name}!"
end
end
class Programmer
def bake(food_name)
"I don't know how to bake #{food_name} :("
@shime
shime / cooks.rb
Created August 6, 2012 11:50
overriding instance method with module, fourth solution
class Programmer
def bake(food_name)
"I don't know how to bake #{food_name} :("
end
end
class Programmer
alias_method :_real_bake, :bake
def bake(food_name)
"I'm baking the #{food_name}!"
@shime
shime / cooks.rb
Created August 6, 2012 11:43
overriding instance method with module, third solution
module CanCook
def self.included(base)
base.class_eval do
remove_method :bake
end
end
def bake(food_name)
"I'm baking the #{food_name}!"
end
end
@shime
shime / cooks.rb
Created August 6, 2012 11:36
overriding instance method with module, second solution
module CanCook
def bake(food_name)
"I'm baking the #{food_name}!"
end
end
class Programmer
def bake(food_name)
"I don't know how to bake #{food_name} :("
end