Skip to content

Instantly share code, notes, and snippets.

@melborne
melborne / flickrup.rb
Created September 30, 2009 06:10
Post to Flickr
#!/opt/local/bin/ruby
require "rubygems"
require "exifr"
require "flickr"
require "pit"
class FlickrPhoto
def self.set_token(*args)
@@flickr = Flickr.new(*args)
if @@flickr.auth.token
@melborne
melborne / dijkstra.rb
Created January 21, 2010 08:18
Dijkstra method
#!/usr/bin/env ruby -wKU
# -*- encoding:utf-8 -*-
class Node
attr_accessor :id, :edges, :cost, :done, :from
def initialize(id, edges=[], cost=nil, done=false)
@id, @edges, @cost, @done = id, edges, cost, done
end
end
@melborne
melborne / graphaz.rb
Created February 3, 2010 10:42
GraphViz wrapper to make animation gif
require "graphviz"
require "RMagick"
class GraphAz
attr_accessor :graph, :nodes, :edges, :gnode, :gedge
def initialize(name= :G, *opt, &blk)
@graph = GraphViz.new(name, *opt, &blk)
@gnode, @gedge = @graph.node, @graph.edge
@nodes, @edges = [], {}
@@lap, @laps = 0, []
@melborne
melborne / graphviz_attributes.rb
Created February 4, 2010 10:13
Graphviz Sampler
# -*- encoding:utf-8 -*-
require "graphaz"
class Sampler
@@q = Hash.new{[]}
def self.initialize
flag, color = nil, nil
data = DATA.readlines.map { |line| line.chomp }
data.each do |item|
next if item.empty?
@melborne
melborne / rand_graph.rb
Created February 6, 2010 12:42
Graphviz Sampler
# -*- encoding:utf-8 -*-
require "graphaz"
COLORS = DATA.readlines.map { |line| line.sub(/\[.*$/, '').chomp }
DARKS = %w(black urlywood navyblue navy indigo darkslateblue mediumblue midnightblue blue darkslategray)
def init(ga, num)
1.upto(num) { |n| ga.add(n.to_s) }
1.upto(num) { |n| ga.add("#{rand(num)+1} => #{n}") }
ga
@melborne
melborne / default_replace.rb
Created February 7, 2010 12:29
Termtter plugins
module Termtter::Client
# search replacement:
# ADD: #[page] arg for list next pages
register_command(
:name => :search, :aliases => [:s],
:exec_proc => lambda {|arg|
search_option = config.search.option.empty? ? {} : config.search.option
arg.gsub!(/\s*#(\d+)$/) { search_option[:page] = $1 ; ''}
if arg.empty? && tags = public_storage[:hashtags]
arg = tags.to_a.join(" ")
@melborne
melborne / bloopsong.rb
Created May 27, 2010 14:25
DSL for bloopaphone
require "bloops"
class BloopSong
def self.init(tempo)
@bloops = Bloops.new
@bloops.tempo = tempo
yield self
self
end
@melborne
melborne / class_tree.rb
Created September 30, 2010 02:38
Ruby Class Tree
#!/usr/bin/env ruby -wKU
def class_tree(klass, mod=false)
klass = klass.class unless klass.is_a?(Class)
supers = klass.ancestors.reject { |anc| anc.class == Module unless mod }
method_list = supers.inject({}) do |h, _class|
h[_class] = _class.public_instance_methods(false).
sort.partition { |m| m !~ /^\w/ }.flatten
h
end
@melborne
melborne / sorts.rb
Created October 12, 2010 13:19
Sort Algorithms in Ruby
#!/opt/local/bin/ruby1.9
#-*-encoding: utf-8-*-
require "test/unit"
class Array
def insert_sort
inject([]) { |mem, var| mem.insert_with_order(var) }
end
def insert_with_order(item)
@melborne
melborne / searchs.rb
Created October 16, 2010 22:33
String Search Algorithms in Ruby
#!/opt/local/bin/ruby1.9
#-*-encoding: utf-8-*-
require "test/unit"
require "strscan"
class String
def self.method_added(name)
class_variable_set("@@#{$`}", 0) if name =~ /_compare$/
end