Setup remote repository:
ssh [email protected]
mkdir my_project.git
cd my_project.git
git init --bare
On local machine:
cd my_project
A = Hash.new { |a,(m,n)| a[[m,n]] = m==0 ? n+1 : n==0 ? a[[m-1,1]] : a[[m-1, a[[m, n-1]]]] } #=> {} | |
A[[3,4]] #=> 125 | |
A.inspect #=> ... long |
# importer/exporter interaction via fibers | |
require 'fiber' | |
class Importer | |
DATA = [ | |
["foo", "bar"], | |
["jazz", "quux"], | |
["cats", "dogs"] | |
] |
Setup remote repository:
ssh [email protected]
mkdir my_project.git
cd my_project.git
git init --bare
On local machine:
cd my_project
# lib/attachable_sti_type.rb | |
class AttachableStiType < ActiveRecord::Type::String | |
def cast_value(value) | |
if value =~ /\A([a-z]+)\/([a-z]+)\Z/ | |
"#{$2}/#{$1}_record".classify | |
else | |
super | |
end | |
end |
class UniqueCollectedValidator < ActiveRecord::Validations::UniquenessValidator | |
def initialize(options) | |
unless options[:within].present? | |
raise ArgumentError, ":within option is required to determine the unique scope's collection." | |
end | |
unless Array(options[:within]).all? { |within| within.respond_to?(:to_sym) } | |
raise ArgumentError, "#{options[:within]} is not a supported format for the :within option. " \ | |
"Pass a symbol or an array of symbols instead: `within: :profiles`" | |
end | |
super |
[:foo, :bar].map(&{ bar: "BAZ", foo: "QUUX" }) |
class Invoice < ApplicationRecord | |
include JsonModel::Attribute | |
json_attribute_model :payment, Payment | |
validates_associated :payment | |
end |
def list((head, *tail)) | |
case tail.length | |
when 0 | |
head&.fetch(:name).to_s | |
when 1 | |
"#{list [head]} & #{list tail}" | |
else | |
"#{list [head]}, #{list tail}" | |
end | |
end |
require 'rubygems' | |
require 'nokogiri' | |
require 'csv' | |
f = File.open("table.html") | |
doc = Nokogiri::HTML(f) | |
CSV.open("output.csv", 'w') do |csv| | |
doc.xpath('//table/tbody/tr').each do |row| | |
tarray = [] |