module Erlang

Constants

EmptyAtom

The canonical empty `Atom`. Returned by `Atom[]` when invoked with no arguments; also returned by `Atom.empty`. Prefer using this one rather than creating many empty atoms using `Atom.new`.

@private

EmptyBinary

The canonical empty `Binary`. Returned by `Binary[]` when invoked with no arguments; also returned by `Binary.empty`. Prefer using this one rather than creating many empty binaries using `Binary.new`.

@private

EmptyMap

The canonical empty `Map`. Returned by `Map[]` when invoked with no arguments; also returned by `Map.empty`. Prefer using this one rather than creating many empty hashes using `Map.new`.

@private

EmptyString

The canonical empty `Erlang::String`. Returned by `Erlang::String[]` when invoked with no arguments; also returned by `Erlang::String.empty`. Prefer using this one rather than creating many empty strings using `Erlang::String.new`.

@private

EmptyTrie

@private

EmptyTuple

The canonical empty `Tuple`. Returned by `Tuple[]` when invoked with no arguments; also returned by `Tuple.empty`. Prefer using this one rather than creating many empty tuples using `Tuple.new`.

@private

FalseAtom

@private

NegativeInfinity
Nil

A list without any elements. This is a singleton, since all empty lists are equivalent.

NilAtom

@private

PositiveInfinity
TrueAtom

@private

Public Class Methods

compare(a, b) click to toggle source
# File lib/erlang/terms.rb, line 55
def self.compare(a, b)
  a = Erlang.from(a)
  b = Erlang.from(b)
  t = type(a)
  c = Erlang::Terms::TERM_ORDER[t] <=> Erlang::Terms::TERM_ORDER[type(b)]
  return c if c != 0
  case t
  when :atom
    return Erlang::Atom.compare(a, b)
  when :bitstring
    return Erlang::Bitstring.compare(a, b)
  when :fun
    return Erlang::Function.compare(a, b)
  when :list
    return Erlang::List.compare(a, b)
  when :map
    return Erlang::Map.compare(a, b)
  when :nil
    return 0
  when :number
    if is_float(a) or is_float(b)
      af = a
      if not is_float(a)
        begin
          af = Erlang::Float[a]
        rescue ArgumentError
          af = a
        end
      end
      bf = b
      if not is_float(b)
        begin
          bf = Erlang::Float[b]
        rescue ArgumentError
          bf = b
        end
      end
      return Erlang::Float.compare(af, bf) if af.kind_of?(Erlang::Float) and bf.kind_of?(Erlang::Float)
      return af.data <=> bf if af.kind_of?(Erlang::Float) and not bf.kind_of?(Erlang::Float)
      afbfcmp = bf.data <=> af
      return -afbfcmp
    end
    return a <=> b
  when :pid
    return Erlang::Pid.compare(a, b)
  when :port
    return Erlang::Port.compare(a, b)
  when :reference
    return Erlang::Reference.compare(a, b)
  when :tuple
    return Erlang::Tuple.compare(a, b)
  else
    raise NotImplementedError
  end
end
from(term) click to toggle source
# File lib/erlang/terms.rb, line 111
def self.from(term)
  return term.to_erlang if term.respond_to?(:to_erlang)
  # integer
  return term if is_integer(term)
  # float
  return term if term.kind_of?(Erlang::Float)
  return Erlang::Float[term] if is_float(term)
  # atom
  return term if term.kind_of?(Erlang::Atom)
  return Erlang::Atom[term] if term.kind_of?(::Symbol)
  return Erlang::Atom[term] if term.kind_of?(::FalseClass)
  return Erlang::Atom[term] if term.kind_of?(::NilClass)
  return Erlang::Atom[term] if term.kind_of?(::TrueClass)
  # reference, function, port, pid, and tuple
  return term if is_reference(term)
  return term if is_function(term)
  return term if is_port(term)
  return term if is_pid(term)
  return term if is_tuple(term)
  # map
  return term if term.kind_of?(Erlang::Map)
  return Erlang::Map[term] if term.kind_of?(::Hash)
  # nil
  return term if term.equal?(Erlang::Nil)
  return Erlang::Nil if is_list(term) and term.empty?
  # list
  return term if term.kind_of?(Erlang::List)
  return term if term.kind_of?(Erlang::String)
  return Erlang::List.from_enum(term) if term.kind_of?(::Array)
  # bitstring
  return term if term.kind_of?(Erlang::Binary)
  return term if term.kind_of?(Erlang::Bitstring)
  return Erlang::Binary[term] if term.kind_of?(::String)
  raise ArgumentError, "unable to convert ruby object of class #{term.class} to erlang term"
end
inspect(term = Erlang::Undefined, raw: false) click to toggle source
Calls superclass method
# File lib/erlang/terms.rb, line 147
def self.inspect(term = Erlang::Undefined, raw: false)
  return super() if Erlang::Undefined.equal?(term)
  term = from(term)
  return term.erlang_inspect(raw) if term.respond_to?(:erlang_inspect)
  return term.inspect if is_any(term)
  raise NotImplementedError
end
iolist_to_binary(iolist) click to toggle source
# File lib/erlang/terms.rb, line 155
def self.iolist_to_binary(iolist)
  return iolist if is_binary(iolist)
  return Erlang::Binary.new(iolist) if iolist.is_a?(::String)
  return Erlang::Binary.new(iolist.to_s) if iolist.is_a?(::Symbol)
  if is_list(iolist)
    return Erlang::Binary.new(iolist.flatten.map do |element|
      data = nil
      if element.is_a?(::Integer) and element <= 255 and element >= -256
        element = element + 256 if element < 0
        data = element.chr
      elsif element.is_a?(::String)
        data = element
      elsif is_binary(element)
        data = element.data
      elsif element.is_a?(::Symbol)
        data = element.to_s
      elsif is_list(element)
        data = iolist_to_binary(element).data
      else
        raise ArgumentError, "unknown element in iolist: #{element.inspect}"
      end
      next Erlang::Terms.binary_encoding(data)
    end.join)
  else
    raise ArgumentError, "unrecognized iolist: #{iolist.inspect}"
  end
end
is_any(term) click to toggle source
# File lib/erlang/terms.rb, line 183
def self.is_any(term)
  return true if is_atom(term)
  return true if is_bitstring(term)
  return true if is_boolean(term)
  return true if is_float(term)
  return true if is_function(term)
  return true if is_integer(term)
  return true if is_list(term)
  return true if is_map(term)
  return true if is_number(term)
  return true if is_pid(term)
  return true if is_port(term)
  return true if is_reference(term)
  return true if is_tuple(term)
  return false
end
is_atom(term) click to toggle source
# File lib/erlang/terms.rb, line 200
def self.is_atom(term)
  return true if term.kind_of?(::Symbol)
  return true if term.kind_of?(::FalseClass)
  return true if term.kind_of?(::NilClass)
  return true if term.kind_of?(::TrueClass)
  return true if term.kind_of?(Erlang::Atom)
  return false
end
is_binary(term) click to toggle source
# File lib/erlang/terms.rb, line 209
def self.is_binary(term)
  return true if term.kind_of?(::String)
  return true if term.kind_of?(Erlang::Binary)
  return false
end
is_bitstring(term) click to toggle source
# File lib/erlang/terms.rb, line 215
def self.is_bitstring(term)
  return true if is_binary(term)
  return true if term.kind_of?(Erlang::Bitstring)
  return false
end
is_boolean(term) click to toggle source
# File lib/erlang/terms.rb, line 221
def self.is_boolean(term)
  return true if term.kind_of?(::FalseClass)
  return true if term.kind_of?(::TrueClass)
  return true if term == :true
  return true if term == :false
  return false
end
is_float(term) click to toggle source
# File lib/erlang/terms.rb, line 229
def self.is_float(term)
  return true if term.kind_of?(::BigDecimal)
  return true if term.kind_of?(::Float)
  return true if term.kind_of?(::Rational)
  return true if term.kind_of?(Erlang::Float)
  return false
end
is_function(term, arity = nil) click to toggle source
# File lib/erlang/terms.rb, line 237
def self.is_function(term, arity = nil)
  if arity == nil
    return true if term.kind_of?(Erlang::Export)
    return true if term.kind_of?(Erlang::Function)
  else
    return true if is_function(term) and term.arity == arity
  end
  return false
end
is_integer(term) click to toggle source
# File lib/erlang/terms.rb, line 247
def self.is_integer(term)
  return true if term.kind_of?(::Integer)
  return false
end
is_list(term) click to toggle source
# File lib/erlang/terms.rb, line 252
def self.is_list(term)
  return true if term.kind_of?(Erlang::List)
  return true if term.kind_of?(Erlang::String)
  return true if term.kind_of?(::Array)
  return true if term.equal?(Erlang::Nil)
  return false
end
is_map(term) click to toggle source
# File lib/erlang/terms.rb, line 260
def self.is_map(term)
  return true if term.kind_of?(Erlang::Map)
  return true if term.kind_of?(::Hash)
  return false
end
is_number(term) click to toggle source
# File lib/erlang/terms.rb, line 266
def self.is_number(term)
  return true if is_float(term) or is_integer(term)
  return false
end
is_pid(term) click to toggle source
# File lib/erlang/terms.rb, line 271
def self.is_pid(term)
  return true if term.kind_of?(Erlang::Pid)
  return false
end
is_port(term) click to toggle source
# File lib/erlang/terms.rb, line 276
def self.is_port(term)
  return true if term.kind_of?(Erlang::Port)
  return false
end
is_reference(term) click to toggle source
# File lib/erlang/terms.rb, line 281
def self.is_reference(term)
  return true if term.kind_of?(Erlang::Reference)
  return false
end
is_tuple(term) click to toggle source
# File lib/erlang/terms.rb, line 286
def self.is_tuple(term)
  return true if term.kind_of?(Erlang::Tuple)
  return false
end
type(term) click to toggle source
# File lib/erlang/terms.rb, line 291
def self.type(term)
  return :atom if is_atom(term)
  return :bitstring if is_bitstring(term)
  return :fun if is_function(term)
  return :list if is_list(term) and not term.empty?
  return :map if is_map(term)
  return :nil if is_list(term) and term.empty?
  return :number if is_number(term)
  return :pid if is_pid(term)
  return :port if is_port(term)
  return :reference if is_reference(term)
  return :tuple if is_tuple(term)
  raise NotImplementedError
end