Skip to content

Instantly share code, notes, and snippets.

View workmad3's full-sized avatar

David Workman workmad3

View GitHub Profile
module Fuse
module Helper
def self.run_later(run_context, &blk)
run_context.ruby_block("Run-later #{SecureRandom.uuid}") do
block &blk
end
end
end
end
class Colour
attr_reader :red, :green, :blue
def initialize(red, green, blue, maxval)
@red, @green, @blue, @maxval = red, green, blue, maxval
end
def ansii_24
"\033[48;2;#{@red};#{@green};#{@blue}m \033[39;49m"
end
class Port
def initialize(slot, port_spec)
@slot = slot
@spec = port_spec
end
end
class Slot
def self.create(slot_spec)
@spec = slot_spec
def modal(title, &body)
capture do
concat(render "shared/modal-header", title: tile)
concat(capture(&body))
concat(render "shared/modal-footer")
end
end
source = ["NewsArticle", "NewsArticle", "NewsArticle", "NewsArticle", "Event", "Event", "Tweet", "Tweet", "Event"]
sorted = []
%w(NewsArticle Event Tweet).cycle do |current_type|
sorted << source.delete_at(source.index(current_type)) if source.include?(current_type)
break if source.empty?
end
@workmad3
workmad3 / bm.rb
Last active August 29, 2015 14:14
require 'benchmark'
hsh = {}
1000.times {|i| hsh[i.to_s] = i.succ.to_s}
Benchmark.bm do |x|
x.report("Hash[]") {1000.times{Hash[hsh.values.zip(hsh.keys)]}}
x.report("invert") {1000.times{hsh.invert}}
x.report("each_with_object") {1000.times{hsh.each_with_object({}){|(k,v),h| h[v] = k}}}
end
the path /etc/sv/service_name/env contains the environment, in the form of files. The name of the file is the variable name, the contents are the variable's value, e.g.
file: /etc/sv/service_name/env/RUBY_VERSION
contents: 2.1.5
input = [
['a', 'b', 'c'],
['d', 'e'],
['c', 'e'],
['f', 'g'],
['g', 'j']
]
outputs = input.each_with_object([]) do |ary, output|
merge_with = output.find_all{|o| (o & ary).size > 0 }
def foo(&block)
block = ->{"c"}
p block.call
p yield
end
@workmad3
workmad3 / equality_bases
Created January 15, 2015 10:22
Quick method as an exercise to find a pair of bases that make two strings representing numbers equal.
def find_equality_bases(a,b,max_base=100)
a_parts = a.to_s.split("").map{|i| i.to_i(36)}.reverse
b_parts = b.to_s.split("").map{|i| i.to_i(36)}.reverse
a_start_base = a_parts.max + 1
b_start_base = b_parts.max + 1
if a == b
return [start_base, start_base]
end
(a_start_base..max_base).each do |a_base|
(b_start_base..max_base).each do |b_base|