Skip to content

Instantly share code, notes, and snippets.

@melborne
melborne / filter.rb
Last active August 29, 2015 13:57
Pipeline in Ruby
class Filter
class << self
def filters
@filters ||= {}
end
def add(name, &code)
filters[name] = code
end
end
# Gviz sample:
# Graphviz Example: Undirected Graph Clusters | Graphviz - Graph Visualization Software
# http://www.graphviz.org/content/fdpclust
require "gviz"
Graph(:G, :graph) do
global layout:'fdp'
node :e
subgraph(:clusterA) do
route :a => :b
@melborne
melborne / election.rb
Last active August 29, 2015 13:55
HTML-like labels sample for Gviz
#election.rb
require "gviz"
header, *candidates = DATA.readlines.map(&:split)
Graph do
global layout:'fdp'
nodes shape:"plaintext", style:"filled", fillcolor:"white"
node :election, shape:'doublecircle', label:"2014年\n東京都知事選挙", fillcolor:'orange'
@melborne
melborne / animetize.rb
Last active January 3, 2016 07:29
Examples for Matreska gem
require "RMagick"
include Magick
images = Dir.glob("*.png").sort_by { |n| n.sub(/\D+/,'').to_i }
anim = ImageList.new(*images)
anim.delay = 40
anim.write("anpan.gif")
# Batman equation for Gviz
# [geometry - Is this Batman equation for real? - Mathematics Stack Exchange](http://math.stackexchange.com/questions/54506/is-this-batman-equation-for-real 'geometry - Is this Batman equation for real? - Mathematics Stack Exchange')
def f1(x)
Math.sqrt(1 - (x/7.0)**2) * 3
end
def f2(x)
(x/2.0).abs - ((3*Math.sqrt(33)-7)/112.0)*x**2 - 3 + Math.sqrt((1 - ((x.abs - 2).abs - 1)**2).abs)
end
@melborne
melborne / snow.rb
Last active December 31, 2015 03:38
Translate Let it Snow in the Terminal from Ruby to Ruby
# Translate Let it Snow in the Terminal from Ruby to Ruby
#
# Original code:
# ruby -e 'C=`stty size`.scan(/\d+/)[1].to_i;S=["2743".to_i(16)].pack("U*");a={}; \
# puts "\e[2J";loop{a[rand(C)]=0;a.each{|x,o|;a[x]+=1;print "\e[#{o};#{x}H \
# \e[#{a[x]};#{x}H#{S} \e[0;0H"};$stdout.flush;sleep 0.1}'
#
# Let it Snow in the Terminal of Mac OS X with This Command
# http://osxdaily.com/2013/12/06/snow-terminal-mac-os-x-command/
#
@melborne
melborne / dir.rb
Created October 21, 2013 00:58
Visualize file directory structure with gviz (graphviz ruby wrapper)
class F
attr_reader :name, :path, :level
def initialize(name, level=0)
@name = File.basename(name)
@path = File.expand_path(name)
@level = level
end
def to_s
"F: #{name}"
@melborne
melborne / ruby_tree.rb
Created October 18, 2013 09:51
Draw Ruby universe with gviz(graphviz wrapper in Ruby)
# Draw Ruby universe with gviz(graphviz wrapper in Ruby)
# try `gviz build [THIS_FILE]` at terminal
classes = ObjectSpace.each_object(Class)
.reject { |k| "#{k}".match /Gem|Thor|Gviz/ }
global layout:'fdp'
nodes style:'filled', colorscheme:'purd6'
edges color:'maroon'
# coding=utf-8
# This is an edited version of @ne_sachirou's String::INFINITY
# Range 'a' .. String::INFINITY (Ruby) - c4se記:さっちゃんですよ☆
# http://c4se.hatenablog.com/entry/2013/10/01/010305
# Diffs:
# - Define StringInfinity#inspect
# - Use Module#prepend
# - Replace '==' to 'eqlual?' at #size to fix when last=='' passed
# - Fix Range#size test
module AutoAttrSet
def new(*args, &blk)
obj = allocate
obj.send(:initialize, *args, &blk)
set_instance_variables(obj, args)
obj
end
private
def set_instance_variables(obj, args)