Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created October 6, 2008 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenderlove/15180 to your computer and use it in GitHub Desktop.
Save tenderlove/15180 to your computer and use it in GitHub Desktop.
####
# ( ++ B ).to_i #=> 3
# ( ++- B ).to_i #=> 6
# ( ++-+ B ).to_i #=> 13
# ( 0 + ++-+ B ) #=> 13
class B
class << self
def +@; new.send(:"+@"); end
def -@; new.send(:"-@"); end
end
def initialize
@stack = []
end
def +@
@stack << 1
self
end
def -@
@stack << 0
self
end
def to_i
@stack.reverse.inject(0) { |m,v| (m << 1) ^ v }
end
def coerce o
[o, to_i]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment