Skip to content

Instantly share code, notes, and snippets.

@yugui
yugui / gist:30428
Created November 30, 2008 12:47
helper methods to generate mdoc from OptionParser in Ruby
require 'optparse'
class OptionParser
def list_opts(prior_opts)
opts = enum_for(:visit, :tap).map(&:list).flatten(1)
prior_opts = opts.select{|opt| opt.long.any?{|name| prior_opts.include?(name)} }
opts -= prior_opts
simple_opts = opts.select{|opt| !opt.short.empty? && opt.arg.nil? }.sort_by{|x| x.short.sort.first}
opts -= simple_opts
@yugui
yugui / gist:1724
Created July 23, 2008 13:39
Decorator for Ruby, inspired by Python
class Module
def declare(decorator, *args)
orig_method_added = method(:method_added)
metaclass = (class << self; self end)
defined_methods = []
metaclass.send(:define_method, :method_added) do |name|
orig_method_added.call(name)
defined_methods << name
end
begin