This file contains 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
module CodeMigrations | |
# Number isn't important, vague syntax, and not all | |
# of these are specifically Rails examples | |
version from: 4.0, to: 5.0 | |
# The easiest case, literally A to B direct translation. | |
class FilterToAction < Migration | |
expect_translation( | |
"after_filter" => "after_action", | |
# ...snipped excess for example |
This file contains 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 "rubocop" | |
def tree_distance(object_a, object_b) | |
tree_distance_matrix(object_a, object_b).last.last | |
end | |
def tree_distance_matrix(object_a, object_b) | |
a_size, b_size = object_a.size, object_b.size | |
# Rows and columns ascend from 0 to size + 1 |
This file contains 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 'proc_to_ast' | |
def where(&fn) | |
ast = fn.to_ast | |
source = fn.to_source | |
pp ast:, source: | |
end | |
where { age > 18 && occupation.downcase == 'plumber' } |
This file contains 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
c = '' << 101 | |
SUB = /___/ | |
subs = -> s { s.gsub(SUB, c) } | |
fn = "/tmp/runs.rb" | |
# Patch it in for fun | |
`#{c}cho 'alias run #{subs['___val']}' > #{fn}` | |
load fn | |
mm = <<~ABC |
This file contains 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
# Pattern to match a link: <a/> | |
node in { name: 'a' } | |
# Pattern for a link with a class: <a class="foo"/> | |
node in { name: 'a', class: 'foo' } | |
# Pattern to select that link: <a class="foo">{.}</a> | |
node => { name: 'a', class: 'foo', children: } | |
# Pattern to select the target URL: <a class="foo" href="{.}"/> |
This file contains 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
module HTML | |
VALID_TAGS = %i(html p div span a ol ul li strong em table thead tr td th tbody article aside) | |
SELF_CLOSING_TAGS = %i(img br hr) | |
# Evaluates a block passed to it for HTML tags. This is done to isolate | |
# defined tag methods from the outside world, hence `instance_eval` to | |
# evaluate that block in the context of an HTML structure. | |
class Evaluator | |
attr_reader :contents |
This file contains 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/ips' | |
class TimeFilter | |
attr_reader :start, :finish | |
def initialize(start, finish) | |
@start = start | |
@finish = finish | |
end |
This file contains 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 Card | |
SUITS = %w(S H D C).freeze | |
RANKS = %w(2 3 4 5 6 7 8 9 10 J Q K A).freeze | |
RANKS_SCORES = RANKS.each_with_index.to_h | |
include Comparable | |
attr_reader :suit, :rank | |
def initialize(suit, rank) |
This file contains 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
# sudo gem install bson | |
# sudo gem install bson_ext | |
# sudo gem install yajl-ruby | |
# sudo gem install json | |
# sudo gem install msgpack | |
require 'rubygems' | |
require 'benchmark' | |
require 'yaml' | |
require 'bson' |
This file contains 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
divisible_by = -> n, v { v % n == 0 }.curry | |
(1..100).each { |v| | |
puts case v | |
when divisible_by[15] then 'FizzBuzz' | |
when divisible_by[3] then 'Fizz' | |
when divisible_by[5] then 'Buzz' | |
else | |
v | |
end |