Skip to content

Instantly share code, notes, and snippets.

@collin
Created August 31, 2008 23:40
Show Gist options
  • Save collin/8238 to your computer and use it in GitHub Desktop.
Save collin/8238 to your computer and use it in GitHub Desktop.
#
# Logical inverter!
require 'rubygems'
require 'ruby2ruby'
class Sexp
def to_ruby
Ruby2Ruby.new.process(self)
end
def invert_logic
Sexp.from_array(self.map do |op|
case op
when :or
:and
when :and
:or
else
if op.is_a? Sexp
case op.first
when :not
op.last.invert_logic
when :const
Sexp.for("!#{op.to_ruby}")
else
op.invert_logic
end
else
op
end
end
end.compact)
end
end
if $0 == __FILE__
puts Sexp.for("(A or B)").invert_logic.to_ruby
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment