Skip to content

Instantly share code, notes, and snippets.

@WizardOfOgz
WizardOfOgz / gist:1012107
Created June 7, 2011 12:13
Save Base64-encoded images with Paperclip
class Avatar < ActiveRecord::Base
attr_accessor :content_type, :original_filename, :image_data
before_save :decode_base64_image
has_attached_file :image,
PAPERCLIP_CONFIG.merge(
:styles => {
:thumb => '32x32#',
:medium => '64x64#',
class Foo
has_many :bars
def to_json
bars_json = bars.map(&:to_json).join(",")
%Q({"foo": {...{"bars": [#{bars_json}] } } })
end
end
class Bar
@WizardOfOgz
WizardOfOgz / sums_100.rb
Created May 23, 2015 04:22
Sum digits to 100
def sum_digits_to(n)
enumerator = Enumerator.new do |yielder|
# recursive proc (backtracking) which iterates over all possible
# combinations of digits and operators.
p = -> prev_lhs = "", digit = 1 do
lhs = "#{ prev_lhs }#{ digit }"
if digit == 9
yielder << lhs if eval(lhs) == n
else
[" + ", " - ", nil].each do |op|
@WizardOfOgz
WizardOfOgz / gist:02c0aba02df6216fab38
Created May 29, 2015 16:33
The best way to use ActionView helpers outside of a view.
::ActionView::Helpers::NumberHelper.instance_method(:number_with_precision).bind(Module.new{ extend ActionView::Helpers::NumberHelper }).call("1234.0", :strip_insignificant_zeros => true)
require 'benchmark'
puts RUBY_VERSION
phones = ["(216) 375-9240","(518) 462-8200","(215) 434-2915","(714) 778-4011",
"(512) 474-7773","(805) 395-1301","(301) 659-5500","(504) 346-5900","(617) 275-4600",
"(313) 643-9080", "(203) 579-1525", "(716) 854-9100", "(609) 541-5028", "(704) 372-4900",
"(312) 853-3920", "(213) 637-8883", "(216) 696-5422", "(614) 224-3735", "(214) 742-5114",
"(513) 228-8015", "(213) 956-1391", "(919) 373-8633", "(717) 233-9031", "(203) 525-0155",
"(317) 635-0119", "(213) 645-9011", "(201) 333-0250", "(816) 474-1850", "(702) 387-7000",
@WizardOfOgz
WizardOfOgz / diff-highlight
Last active July 7, 2023 01:18
A script to locate and execute diff-highlight from the current Git installation (my install was done on macOS using Homebrew). Just save this where it can be found on your PATH.
#!/usr/bin/env bash
$(ruby -e 'puts File.expand_path("../../share/git-core/contrib/diff-highlight/diff-highlight", File.realpath(`/usr/bin/which git`.strip))') "$@"