Skip to content

Instantly share code, notes, and snippets.

Avatar

Josh Nesbitt joshnesbitt

View GitHub Profile
View dynamic-error-pages-in-rails.md

It's a little known fact that you can easily substitute the default Rails HTML error pages with something more pleasant. You may have noticed the 404.html, 422.html and 500.html files that are generated with every new Rails project and wondered if there's a clean way to style them like the rest of your application. There is, and it's surprisingly simple.

Basic Implementation

The default status code templates are served by a Rack exception application. You can override this to be any Rack compatible app, including your applications router:

# config/application.rb
config.exceptions_app = self.routes
@joshnesbitt
joshnesbitt / readme.md
Last active May 16, 2016
A simple script to get you on the Mondo beta.
View readme.md

Mondo Referral Bump

Thanks to @cbarratt for writing this. Tested on Ruby 2.2.2.

Installation

Install the dependencies:

gem install capybara ffaker poltergeist selenium-webdriver chromedriver-helper
@joshnesbitt
joshnesbitt / pub_sub.rb
Last active Sep 16, 2015
A very simple pub sub implementation.
View pub_sub.rb
class Hub
class << self
NAMESPACE_SEPARATOR = ':'
def publish(event, payload = {})
handlers[event].each do |handler|
handler.call(payload)
end
end
@joshnesbitt
joshnesbitt / template.rb
Created Aug 20, 2015
Example of a simple templating language written in Ruby.
View template.rb
class VariableLang
def initialize(path)
@path = path
@content = File.read(path)
end
def render(locals = {})
locals.inject(@content.dup) do |buffer, vars|
key = vars.first
View string_gymnastics.rb
price_list = "orange = 10p apple = 20p bread = £1.10 tomato = 25p cereal = £2.34"
list = <<-LIST
list
orange
apple
apple
orange
tomato
cereal
bread
View reopen_class.rb
class MyClass
def one_method
puts "Hey!"
end
end
class MyClass
@joshnesbitt
joshnesbitt / metaclass.rb
Last active Aug 29, 2015
How well do you know Ruby? Can you explain what's going on here? What is MyClass.metaclass returning?
View metaclass.rb
class MyClass
def self.metaclass
class << self; self; end
end
end
puts MyClass.metaclass.inspect
View dsl-with-managing-director.rb
class Company
TooManyManagingDirectorsError = Class.new(Exception)
class Department
attr_reader :company,
:name,
:employees
def initialize(company, name, &block)
@company = company
View filter.rb
module BannedWordFilter
class Filter
class << self
BANNED_WORDS = %w(
fuck
shit
banned
)
View highlight.sh
function code_highlight(){
style=${1:-zmrok}
pbpaste | highlight --syntax=rb --style=$style -O rtf | pbcopy
}