Skip to content

Instantly share code, notes, and snippets.

@apeiros
apeiros / car.rb
Created August 28, 2008 08:04 — forked from mlangenberg/car.rb
module Lib
def has_color
define_method(:color) do 'red' end
end
end
class Car
extend Lib
has_color
end
@apeiros
apeiros / gist:234189
Created November 13, 2009 21:36 — forked from dwt/gist:234180
class Object
def chain_method(with_symbol, &new_method)
old_method = method with_symbol
eigenclass = class << self; self; end
eigenclass.class_eval do
define_method with_symbol do |*args| new_method[old_method, *args] end
end
end
end
@apeiros
apeiros / FIGHT!.txt
Created December 11, 2009 07:51 — forked from epitron/FIGHT!.txt
$ time ruby1.8 bits.rb 16 > /dev/null
real 0m9.861s
user 0m8.977s
sys 0m0.872s
$ time ruby1.9 bits.rb 16 > /dev/null
real 0m1.742s
user 0m1.740s
#!/usr/bin/ruby -w
module Kernel
ValidLocalVariableName = /\A[a-z_]\w*\z/
def empty_binding
proc{ binding }.call
end
# Dynamically define a bunch of local variables from a hash.
h,m,s = *"23:34:02".match(/\A(\d+):(\d\d):(\d\d)\z/).captures
Time.now+h.to_i*3600+m.to_i*60+s.to_i
@apeiros
apeiros / method_cop.rb
Created October 31, 2011 23:06 — forked from babney/method_cop.rb
MethodCop
module MethodCop
# guard methods that have side effects with a callback that fires before the method is invoked. If the callback returns a "falsey" value,
# the method is halted and will not be called. The callback will return nil instead.
# if the method does not have side effects or you depend on its return value, you should NOT use this on that method!
def guard_method(guarded_method, guard=nil, &callback)
# normalize guard
guard = method(guard) if guard.is_a?(Symbol)
guard = callback if callback
raise ArgumentError, "You can only supply either a guard argument or a block" if block && guard
@apeiros
apeiros / binary_tree.rb
Last active February 8, 2018 16:10 — forked from CodePint/binary_tree.rb
binary tree and nodes
class Node
attr_accessor :data, :left, :right
def initialize(data)
@left = nil
@right = nil
@data = data
end
include Comparable