This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node | |
attr_accessor :data, :left, :right | |
def initialize(data) | |
@left = nil | |
@right = nil | |
@data = data | |
end | |
include Comparable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Lib | |
def has_color | |
define_method(:color) do 'red' end | |
end | |
end | |
class Car | |
extend Lib | |
has_color | |
end |