Skip to content

Instantly share code, notes, and snippets.

@gonzedge
gonzedge / application_controller.rb
Created January 5, 2012 02:34
Rails 3.1 - Adding custom 404 and 500 error pages
class ApplicationController < ActionController::Base
# ...
unless Rails.application.config.consider_all_requests_local
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 }
end
private
def render_error(status, exception)
@gonzedge
gonzedge / scanning.rb
Created February 1, 2017 02:04
Scan test
#!/usr/bin/env ruby
require 'rambling-trie'
trie = Rambling::Trie.create '/usr/share/dict/web2'
puts trie.scan 'beaut' # =>
# beauteous
# beauteously
# beauteousness
# beauti
@gonzedge
gonzedge / match_prefix_benchmark.rb
Last active January 23, 2017 02:05
Benchmark comparing fast_trie, triez and rambling-trie for a match prefix example
#!/usr/bin/env ruby
# From https://stackoverflow.com/questions/41521492/faster-way-to-see-if-a-huge-list-of-strings-is-contained-within-another-string/41523304#41523304
# with slight changes
require 'triez'
require 'trie'
require 'rambling-trie'
require 'benchmark'
@gonzedge
gonzedge / run.sh
Last active January 7, 2017 17:39
Find all substrings that are words with a(n uncompressed) trie
#!/usr/bin/env bash
chmod +x ./substrings.rb
# with the words with friends dictionary found in
# https://github.com/gonzedge/rambling-trie/blob/master/assets/dictionaries/words_with_friends.txt
./substrings.rb 'ifdxawesome45someword3' # =>
# aw
# awe
# awes
def self.delegate delegated_methods_to_delegated_to
delegated_methods_to_delegated_to.each do |methods, delegated_to|
methods.each do |method|
define_method method do |*args|
send(delegated_to).send method, *args
end
end
end
end
@gonzedge
gonzedge / each.rb
Created July 28, 2012 19:44
Version 0.4.2 of the Rambling Trie has been released!
trie.each do |word|
puts "#{word} is included!"
end
@gonzedge
gonzedge / application_helper.rb
Created July 28, 2012 19:13
Pygmentizing your Rails app hosted on Heroku
module ApplicationHelper
PYGMENTIZE_URL = URI.parse 'http://pygmentize.herokuapp.com/'
def highlight_code(code, language)
sha = Digest::SHA1.hexdigest code
Rails.cache.fetch ['code', language, sha].join('-') do
Net::HTTP.post_form(PYGMENTIZE_URL, lang: language, code: code).body.html_safe
end
end
end
@gonzedge
gonzedge / add.rb
Created July 21, 2012 14:41
Rambling Trie 0.4.1 is out!
trie << 'word' # equivalent to trie.add_branch_from('word')
@gonzedge
gonzedge / application_controller.rb
Created April 27, 2012 14:39
Rails - Custom 404 and 500 pages and the exception_notification gem
class ApplicationController < ActionController::Base
# ...
private
# ...
def notify(exception)
ExceptionNotifier::Notifier.exception_notification(request.env, exception,
data: {message: 'an error happened'}).deliver
@gonzedge
gonzedge / Gemfile
Created April 27, 2012 07:19
Introducing rambling-slider-rails: Easily include the jQuery Rambling Slider on your rails app
gem 'rambling-slider-rails'