Class: Boolean::Expression::Logic

Inherits:
Object
  • Object
show all
Defined in:
lib/boolean/expression/logic.rb

Overview

– Copyleft meh. [meh.paranoid.pk | [email protected]]

This file is part of boolean-expression.

boolean-expression is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

boolean-expression is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with boolean-expression. If not, see <www.gnu.org/licenses/>. ++

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(what) ⇒ Logic

Returns a new instance of Logic.



23
24
25
26
27
28
29
30
# File 'lib/boolean/expression/logic.rb', line 23

def initialize (what)
	@type = case what
		when '!',  /not/i then :not
		when '&&', /and/i then :and
		when '||', /or/i  then :or
		else raise SyntaxError, 'invalid logical operator, logical fallacies everywhere'
	end
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



21
22
23
# File 'lib/boolean/expression/logic.rb', line 21

def type
  @type
end

Instance Method Details

#evaluate(a, b = nil) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/boolean/expression/logic.rb', line 32

def evaluate (a, b=nil)
	case @type
		when :not then !a
		when :and then !!(a && b)
		when :or  then !!(a || b)
	end
end

#to_sObject Also known as: inspect



40
41
42
# File 'lib/boolean/expression/logic.rb', line 40

def to_s
	type.to_s.upcase
end