Module: Rbprolog::ClassMethods
- Defined in:
- lib/rbprolog.rb
Instance Method Summary collapse
- #const_missing(sym) ⇒ Object
-
#keywords(*syms) ⇒ Object
Define the vocabulary of rules and facts.
-
#method_missing(sym, *args) ⇒ Object
Generate rule, fact and deduction based on conventions.
-
#rule(sym, *args, options) ⇒ Object
Internal class method to install instance methods for question and enumerator.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
Generate rule, fact and deduction based on conventions
101 102 103 104 105 106 107 108 109 |
# File 'lib/rbprolog.rb', line 101 def method_missing(sym, *args) if self.syms.include? sym Hash === args.last ? rule(sym, *args) : rule(sym, *args, :if => []) elsif self.syms.include? sym.to_s.chomp('?').to_sym Deduction.new(sym.to_s.chomp('?').to_sym, *args) else super end end |
Instance Method Details
#const_missing(sym) ⇒ Object
96 97 98 |
# File 'lib/rbprolog.rb', line 96 def const_missing(sym) Var.new(sym) end |
#keywords(*syms) ⇒ Object
Define the vocabulary of rules and facts
89 90 91 92 93 94 |
# File 'lib/rbprolog.rb', line 89 def keywords(*syms) raise if syms.any? {|sym| sym.to_s.end_with? '?'} self.syms ||= [] self.syms.concat(syms) end |
#rule(sym, *args, options) ⇒ Object
Internal class method to install instance methods for question and enumerator
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/rbprolog.rb', line 112 def rule(sym, *args, ) self.rules ||= [] self.rules << Rule.new(sym, *args, [:if]) unless method_defined?(sym) define_method("#{sym}!") do |*args| deduction = Deduction.new(sym, *args) deduction.extend(Enumerable) rules = self.rules deduction.define_singleton_method(:each) do |&block| each_deduce(Context.new, rules, []) do |hash| block.call hash end end deduction end define_method("#{sym}?") do |*args| self.send("#{sym}!", *args).any? {|hash| true} end end end |