Class: Gamefic::Expression

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

Overview

A tokenization of an input from available syntaxes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verb, tokens) ⇒ Expression

Returns a new instance of Expression.

Parameters:



15
16
17
18
# File 'lib/gamefic/expression.rb', line 15

def initialize verb, tokens
  @verb = verb
  @tokens = tokens
end

Instance Attribute Details

#tokensArray<String> (readonly)

Returns:



11
12
13
# File 'lib/gamefic/expression.rb', line 11

def tokens
  @tokens
end

#verbSymbol (readonly)

Returns:

  • (Symbol)


8
9
10
# File 'lib/gamefic/expression.rb', line 8

def verb
  @verb
end

Instance Method Details

#compare(other) ⇒ Object

Compare two syntaxes for the purpose of ordering them by relevance while dispatching.



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

def compare other
  if verb == other.verb
    other.tokens.compact.length <=> tokens.compact.length
  else
    (other.verb ? 1 : 0) <=> (verb ? 1 : 0)
  end
end