Class: MiniKraken::Core::Arity

Inherits:
Struct
  • Object
show all
Defined in:
lib/mini_kraken/core/arity.rb

Overview

The arity is the number of arguments a relations or a function can take.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#highObject

Returns the value of attribute high

Returns:

  • (Object)

    the current value of high



6
7
8
# File 'lib/mini_kraken/core/arity.rb', line 6

def high
  @high
end

#lowObject

Returns the value of attribute low

Returns:

  • (Object)

    the current value of low



6
7
8
# File 'lib/mini_kraken/core/arity.rb', line 6

def low
  @low
end

Instance Method Details

#==(other) ⇒ Boolean

Equality check

Parameters:

  • other (Arity, Array, Integer)

Returns:

  • (Boolean)

    true if ‘other’ has same boundary values.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mini_kraken/core/arity.rb', line 51

def ==(other)
  return true if object_id == other.object_id

  result = false

  case other
    when Arity
      result = true if (low == other.low) && (high == other.high)
    when Array
      result = true if (low == other.first) && (high == other.last)
    when Integer
      result = true if (low == other) && (high == other)
  end

  result
end

#binary?Boolean

Is the arity set to two?

Returns:

  • (Boolean)

    true if arity is exactly two



28
29
30
# File 'lib/mini_kraken/core/arity.rb', line 28

def binary?
  unique? && low == 2
end

#match?(aCount) ⇒ Boolean

Does the given argument value fits within the boundary values?

Parameters:

  • aCount (Integer)

Returns:

  • (Boolean)


41
42
43
44
45
46
# File 'lib/mini_kraken/core/arity.rb', line 41

def match?(aCount)
  is_matching = aCount >= low
  is_matching &&= aCount <= high unless variadic?

  is_matching
end

#nullary?Boolean

Is the arity set to zero?

Returns:

  • (Boolean)

    true if arity is exactly zero



16
17
18
# File 'lib/mini_kraken/core/arity.rb', line 16

def nullary?
  unique? && low.zero?
end

#unary?Boolean

Is the arity set to one?

Returns:

  • (Boolean)

    true if arity is exactly one



22
23
24
# File 'lib/mini_kraken/core/arity.rb', line 22

def unary?
  unique? && low == 1
end

#unique?Boolean

Is the arity constrained to a single, unique value? In other words, are the low and high bound equal?

Returns:

  • (Boolean)

    true iff both bounds have same value



10
11
12
# File 'lib/mini_kraken/core/arity.rb', line 10

def unique?
  low == high
end

#variadic?Boolean

Can the arity take arbitrary values?

Returns:

  • (Boolean)

    true if arity has no fixed high bound



34
35
36
# File 'lib/mini_kraken/core/arity.rb', line 34

def variadic?
  high == '*'
end