This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
class Watcher | |
def watch | |
loop do | |
if unresponsive? && sleep(3) && unresponsive? | |
restart | |
sleep 20 | |
end | |
sleep 5 | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Enumerator::Lazy | |
def compact | |
reject(&:nil?) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This concern helps controllers redirect to the previous page by storing in session | |
# the current GET request. | |
# | |
# On controllers that need to redirect or that you don't want to keep track of, use: | |
# skip_before_action :store_previous_url | |
# | |
# And then you can redirect to `session[:previous_url]` or use the | |
# `redirect_to_previous_or_root` helper. | |
# | |
# Also, this catches exceptions thrown by `redirect_to :back` when there is |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Markov Chain generator from a text file. | |
# | |
# Example: | |
# source = Markov::FileSource.new('jackson.txt') | |
# chain = Markov::Chain.new(source) | |
# | |
# 10.times do | |
# puts chain.generate_uniq | |
# end | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get phrases | |
phrases = File.readlines('phrases.txt') | |
.map { |p| p.gsub(/["()*]/, '') } | |
.map { |p| p.gsub(/ +/, ' ').strip } | |
.reject { |p| p.split(' ').size < 2 } | |
.uniq | |
# Get first words | |
starters = phrases.map { |p| p.split(' ').first } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require "rmagick" | |
# Addons to CarrierWave to process GIF files. | |
# | |
# Example: | |
# | |
# class AvatarUploader < CarrierWave::Uploader::Base | |
# include CarrierWave::GifProcesses |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Install: place this in your `~/bin/` and call it `ip` for example. | |
# | |
# Usage: | |
# $ ip | |
# 192.168.1.42 | |
# 242.242.42.89 | |
ipconfig getifaddr en0 | |
curl ipecho.net/plain;echo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/email_with_name.rb | |
module EmailWithName | |
def email_with_name(email, name) | |
name = name.gsub(/"<>/, '') | |
"#{name} <#{email}>" | |
end | |
end | |
# app/mailers/application_mailer.rb | |
class ApplicationMailer < ActionMailer::Base |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
def bmark(iterations, benches) | |
size = benches.keys.map(&:length).sort.last | |
Benchmark.bm(size) do |x| | |
benches.each do |key, value| | |
x.report(key) do | |
iterations.times { value.call } | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "jbuilder" | |
# Enable prettification in your `.jbuilder` files | |
# Via https://github.com/rails/jbuilder/issues/195#issuecomment-44440569 | |
# | |
# Example: | |
# json.prettify! if params[:pretty] == "1" | |
# | |
class Jbuilder | |
# Enable or disable prettification |