Class: Erlang::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/erlang/function.rb

Overview

An Function is an internal function. It corresponds to the fun F/A and fun(Arg1,...) -> ... syntax from Erlang.

Creating Functions

Erlang::Function[
  arity: 0,
  uniq: "c>yRz_\xF6\xED?Hv(\x04\x19\x102",
  index: 20,
  mod: :erl_eval,
  old_index: 20,
  old_uniq: 52032458,
  pid: Erlang::Pid["nonode@nohost", 79, 0, 0],
  free_vars: Erlang::List[
    Erlang::Tuple[
      Erlang::Nil,
      :none,
      :none,
      Erlang::List[
        Erlang::Tuple[
          :clause,
          27,
          Erlang::Nil,
          Erlang::Nil,
          Erlang::List[Erlang::Tuple[:atom, 0, :ok]]
        ]
      ]
    ]
  ]
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#arityInteger (readonly)

Return the arity for this Function

Returns:

  • (Integer)


38
39
40
# File 'lib/erlang/function.rb', line 38

def arity
  @arity
end

#free_varsList (readonly)

Return the free variables list for this Function

Returns:



66
67
68
# File 'lib/erlang/function.rb', line 66

def free_vars
  @free_vars
end

#indexInteger (readonly)

Return the index for this Function

Returns:

  • (Integer)


46
47
48
# File 'lib/erlang/function.rb', line 46

def index
  @index
end

#modAtom (readonly)

Return the module for this Function

Returns:



50
51
52
# File 'lib/erlang/function.rb', line 50

def mod
  @mod
end

#old_indexInteger (readonly)

Return the old index for this Function

Returns:

  • (Integer)


54
55
56
# File 'lib/erlang/function.rb', line 54

def old_index
  @old_index
end

#old_uniqInteger (readonly)

Return the old uniq for this Function

Returns:

  • (Integer)


58
59
60
# File 'lib/erlang/function.rb', line 58

def old_uniq
  @old_uniq
end

#pidPid (readonly)

Return the pid for this Function

Returns:



62
63
64
# File 'lib/erlang/function.rb', line 62

def pid
  @pid
end

#uniqInteger (readonly)

Return the uniq for this Function

Returns:

  • (Integer)


42
43
44
# File 'lib/erlang/function.rb', line 42

def uniq
  @uniq
end

Class Method Details

.[](mod:, free_vars:, pid: nil, arity: nil, uniq: nil, index: nil, old_index: nil, old_uniq: nil) ⇒ Function

Create a new Function populated with the given parameters.

Parameters:

  • arity (Integer) (defaults to: nil)

    The arity of the function

  • uniq (::String, Integer) (defaults to: nil)

    The uniq of the function

  • index (Integer) (defaults to: nil)

    The index of the function

  • mod (Atom)

    The module atom

  • old_index (Integer) (defaults to: nil)

    The old index of the function

  • old_uniq (Integer) (defaults to: nil)

    The old uniq of the function

  • pid (Pid) (defaults to: nil)

    The pid of the function

  • free_vars (List)

    The free variables list

Returns:

Raises:

  • (ArgumentError)

    if any of the parameters are of the wrong type or absent



80
81
82
# File 'lib/erlang/function.rb', line 80

def [](mod:, free_vars:, pid: nil, arity: nil, uniq: nil, index: nil, old_index: nil, old_uniq: nil)
  return new(mod: mod, free_vars: free_vars, pid: pid, arity: arity, uniq: uniq, index: index, old_index: old_index, old_uniq: old_uniq)
end

.compare(a, b) ⇒ -1, ...

Compares a and b and returns whether they are less than, equal to, or greater than each other.

Parameters:

Returns:

  • (-1, 0, 1)

Raises:

  • (ArgumentError)

    if a or b is not a Function



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/erlang/function.rb', line 91

def compare(a, b)
  return Erlang::Export.compare(a, b) if a.kind_of?(Erlang::Export) and b.kind_of?(Erlang::Export)
  return -1 if a.kind_of?(Erlang::Function) and b.kind_of?(Erlang::Export)
  return 1 if b.kind_of?(Erlang::Function) and a.kind_of?(Erlang::Export)
  raise ArgumentError, "'a' must be of Erlang::Function type" unless a.kind_of?(Erlang::Function)
  raise ArgumentError, "'b' must be of Erlang::Function type" unless b.kind_of?(Erlang::Function)
  c = Erlang.compare(a.arity, b.arity)
  return c if c != 0
  c = Erlang.compare(a.uniq, b.uniq)
  return c if c != 0
  c = Erlang.compare(a.index, b.index)
  return c if c != 0
  c = Erlang.compare(a.mod, b.mod)
  return c if c != 0
  c = Erlang.compare(a.old_index, b.old_index)
  return c if c != 0
  c = Erlang.compare(a.old_uniq, b.old_uniq)
  return c if c != 0
  c = Erlang.compare(a.pid, b.pid)
  return c if c != 0
  c = Erlang.compare(a.free_vars, b.free_vars)
  return c
end

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Return true if other has the same type and contents as this Function.

Parameters:

  • other (Object)

    The object to compare with

Returns:

  • (Boolean)


173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/erlang/function.rb', line 173

def eql?(other)
  return true if other.equal?(self)
  if instance_of?(other.class)
    return !!(new_function? == other.new_function? &&
      arity == other.arity &&
      uniq == other.uniq &&
      index == other.index &&
      mod == other.mod &&
      old_index == other.old_index &&
      old_uniq == other.old_uniq &&
      pid == other.pid &&
      free_vars == other.free_vars)
  else
    return !!(Erlang.compare(other, self) == 0)
  end
end

#erlang_inspect(raw = false) ⇒ ::String

Return the contents of this Function as a Erlang-readable ::String.

Examples:

# Using the example function at the top of this page
fun.erlang_inspect
# => "{'function',0,<<99,62,121,82,122,95,246,237,63,72,118,40,4,25,16,50>>,20,'erl_eval',20,52032458,{'pid','nonode@nohost',79,0,0},[{[],'none','none',[{'clause',27,[],[],[{'atom',0,'ok'}]}]}]}"

Returns:

  • (::String)


199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/erlang/function.rb', line 199

def erlang_inspect(raw = false)
  if raw == true and Erlang.respond_to?(:term_to_binary)
    result = 'erlang:binary_to_term('
    result << Erlang.inspect(Erlang.term_to_binary(self), raw: raw)
    result << ')'
    return result
  else
    if new_function?
      return Erlang.inspect(Erlang::Tuple[:function, @arity, @uniq, @index, @mod, @old_index, @old_uniq, @pid, @free_vars], raw: raw)
    else
      return Erlang.inspect(Erlang::Tuple[:function, @pid, @mod, @index, @uniq, @free_vars], raw: raw)
    end
  end
end

#inspectString

Returns the nicely formatted version of the Function.

Returns:

  • (String)

    the nicely formatted version of the Function



215
216
217
218
219
220
221
# File 'lib/erlang/function.rb', line 215

def inspect
  if new_function?
    return "Erlang::Function[arity: #{arity.inspect}, uniq: #{uniq.inspect}, index: #{index.inspect}, mod: #{mod.inspect}, old_index: #{old_index.inspect}, old_uniq: #{old_uniq.inspect}, pid: #{pid.inspect}, free_vars: #{free_vars.inspect}]"
  else
    return "Erlang::Function[pid: #{pid.inspect}, mod: #{mod.inspect}, index: #{index.inspect}, uniq: #{uniq.inspect}, free_vars: #{free_vars.inspect}]"
  end
end

#new_function?Boolean

Return true if this is a new function.

Returns:

  • (Boolean)


165
166
167
# File 'lib/erlang/function.rb', line 165

def new_function?
  return !!(!arity.nil?)
end