Class: Lasp::Params

Inherits:
Object
  • Object
show all
Defined in:
lib/lasp/params.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(param_list) ⇒ Params

Returns a new instance of Params.



7
8
9
10
11
# File 'lib/lasp/params.rb', line 7

def initialize(param_list)
  @param_list = param_list

  validate_params!
end

Instance Attribute Details

#param_listObject (readonly)

Returns the value of attribute param_list.



5
6
7
# File 'lib/lasp/params.rb', line 5

def param_list
  @param_list
end

Instance Method Details

#arityObject



28
29
30
# File 'lib/lasp/params.rb', line 28

def arity
  ordered.length.to_s + (variadic? ? "+" : "")
end

#lengthObject



40
41
42
# File 'lib/lasp/params.rb', line 40

def length
  ordered.length
end

#matches_arity?(num_args) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
# File 'lib/lasp/params.rb', line 32

def matches_arity?(num_args)
  if variadic?
    num_args >= length
  else
    num_args == length
  end
end

#orderedObject



13
14
15
# File 'lib/lasp/params.rb', line 13

def ordered
  param_list.take_while { |p| p != :& }
end

#restObject



17
18
19
20
21
22
# File 'lib/lasp/params.rb', line 17

def rest
  unless variadic?
    fail LaspError, "a non-variadic function does not have rest-arguments"
  end
  param_list.last
end

#to_sObject



44
45
46
# File 'lib/lasp/params.rb', line 44

def to_s
  "(" + param_list.join(" ") + ")"
end

#variadic?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/lasp/params.rb', line 24

def variadic?
  param_list.include?(:&)
end