Skip to content

Instantly share code, notes, and snippets.

recursive_hash = lambda { |h,k| h[k] = Hash.new(&recursive_hash) }
table = Hash.new(&recursive_hash)
table['a']['x'] = 1
table['a']['y'] = 2
table['b']['x'] = 3
table['b']['y'] = 4
# {"a"=>{"x"=>1, "y"=>2}, "b"=>{"x"=>3, "y"=>4}}
#
# Demo for how to
# - use memoization with any cache
# - set up invalidation rules
#
class Cache
def initialize
@data = {}
end
x x x x x x x x
o o . o o .
o oo o oo
module A
module B
extend self
def b
"b0"
end
end
module X
extend self
@fronx
fronx / point_free_blocks.rb
Created September 20, 2012 11:52
point-free block passing in ruby
"abcd".method(:length).to_proc.call
# => 4
# ---
def a(x,y)
[x, y]
end
def b
#! /usr/bin/env ruby
require 'json'
STDIN.read.split("\n").each do |line|
data = JSON.parse(line)
puts JSON.pretty_generate(data)
# puts ARGV.map { |k| data[k] }.join(' ')
end
@fronx
fronx / print_aligned.rb
Last active December 10, 2015 12:29
print tables on the console
#!/usr/bin/env ruby
def print_aligned(text, sep)
lines = text.to_s.split("\n").map do |line|
line.split(sep)
end
maxes =
lines.inject([]) do |maxes, line|
line.map(&:length).each_with_index do |length, index|
if length > maxes[index].to_i
c(1,2,3,4,5,6,7)[1:1 + 4 -1]
# => [1] 4
c(1,2,3,4,5,6,7)[1:(1 + 4 -1)]
# => [1] 1 2 3 4
?- Whut.
% ... 1,000,000 ............ 10,000,000 years later
%
% >> 42 << (last release gives the question)
?-
(def rock
{ :snare '(_ O)
:kick '(O _) })
(def dnb
; '(1 _ . _ 2 _ . _ 3 _ . _ 4 _ . _)
{ :snare '(_ _ _ _ O _ _ o _ o _ _ O _ _ _)
:kick '(O _ _ _ _ _ _ _ _ _ O _ _ _ _ _) })
(def reggae