class Erlang::Atom

An `Atom` is a literal constant with a name.

Symbols, Booleans (`true` and `false`), and `nil` are considered `Atom`s in Ruby.

### Creating Atoms

Erlang::Atom["test"]
# => :test
Erlang::Atom[]
# => :Ω
Erlang::Atom[, utf8: true]
# => Erlang::Atom["Ω", utf8: true]
Erlang::Atom[true]
# => true
Erlang::Atom[false]
# => false
Erlang::Atom[nil]
# => nil

Attributes

data[R]

Return the data for this `Atom` @return [::String]

utf8[R]

Return the utf8 flag for this `Atom` @return [::Boolean]

Public Class Methods

[](*data, utf8: false) click to toggle source

Create a new `Atom` populated with the given `data` and `utf8` flag. @param data [::String, Symbol, ::Enumerable, Integer] The content of the `Atom` @param utf8 [Boolean] Whether the `Atom` should be considered UTF-8 or not @return [Atom] @raise [ArgumentError] if `data` cannot be coerced to be a `::String`

# File lib/erlang/atom.rb, line 40
def [](*data, utf8: false)
  return EmptyAtom if data.empty?
  if data.size == 1
    return data[0] if data[0].is_a?(Erlang::Atom)
    return FalseAtom if data[0] == false
    return NilAtom if data[0] == nil
    return TrueAtom if data[0] == true
    if data[0].is_a?(::String)
      data = data[0]
    elsif data[0].is_a?(::Symbol)
      data = data[0].to_s
    end
  end
  unless data.is_a?(::String)
    data = Erlang.iolist_to_binary(data).data
  end
  return FalseAtom if data == "false"
  return NilAtom if data == "nil"
  return TrueAtom if data == "true"
  return new(data, utf8)
end
compare(a, b) click to toggle source

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

@param a [Atom] The left argument @param b [Atom] The right argument @return [-1, 0, 1] @raise [ArgumentError] if `a` or `b` is not an `Atom`

# File lib/erlang/atom.rb, line 98
def compare(a, b)
  raise ArgumentError, "'a' must be of Erlang::Atom type" unless a.kind_of?(Erlang::Atom)
  raise ArgumentError, "'b' must be of Erlang::Atom type" unless b.kind_of?(Erlang::Atom)
  return a.data <=> b.data
end
empty() click to toggle source

Return an empty `Atom`. If used on a subclass, returns an empty instance of that class.

@return [Atom]

# File lib/erlang/atom.rb, line 66
def empty
  return @empty ||= self.new
end
false() click to toggle source

Return a false `Atom`.

@return [Atom]

# File lib/erlang/atom.rb, line 73
def false
  return @false ||= self.new("false")
end
new(data = ::String.new.freeze, utf8 = false) click to toggle source

@private

# File lib/erlang/atom.rb, line 106
def initialize(data = ::String.new.freeze, utf8 = false)
  raise ArgumentError, 'data must be a String' if not data.is_a?(::String)
  @valid_utf8, data = Erlang::Terms.utf8_encoding(data)
  @printable = Erlang::Terms.printable?(data)
  data = Erlang::Terms.binary_encoding(data) if not @printable
  @data = data.freeze
  @utf8 = !!utf8
  if @data == "false"
    @internal = false
  elsif @data == "nil"
    @internal = nil
  elsif @data == "true"
    @internal = true
  else
    @internal = @data.intern
  end
  valid_internal = false
  if @utf8 == false and @valid_utf8 and @printable
    begin
      if @internal == eval(@internal.inspect)
        valid_internal = true
      end
    rescue SyntaxError
      valid_internal = false
    end
  end
  @valid_internal = valid_internal
end
nil() click to toggle source

Return a nil `Atom`.

@return [Atom]

# File lib/erlang/atom.rb, line 80
def nil
  return @nil ||= self.new("nil")
end
true() click to toggle source

Return a true `Atom`.

@return [Atom]

# File lib/erlang/atom.rb, line 87
def true
  return @true ||= self.new("true")
end

Public Instance Methods

==(other)
Alias for: eql?
eql?(other) click to toggle source

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

@param other [Object] The object to compare with @return [Boolean]

# File lib/erlang/atom.rb, line 144
def eql?(other)
  return true if other.equal?(self)
  if instance_of?(other.class)
    return !!(@utf8 == other.utf8 && self.hash == other.hash)
  else
    return !!(Erlang.compare(other, self) == 0)
  end
end
Also aliased as: ==
erlang_inspect(raw = false) click to toggle source

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

@example

Erlang::Atom[:test].erlang_inspect
# => "'test'"
# Pass `utf8: true` for a UTF-8 version of the Atom
Erlang::Atom[].erlang_inspect
# => "'\\xCE\\xA9'"
Erlang::Atom[, utf8: true].erlang_inspect
# => "'Ω'"

@return [::String]

# File lib/erlang/atom.rb, line 174
def erlang_inspect(raw = false)
  if @utf8
    result = '\''
    result << (data.inspect[1..-2].gsub('\''){"\\'"})
    result << '\''
    return result
  else
    result = '\''
    data = @valid_utf8 ? Erlang::Terms.binary_encoding(@data) : @data
    result << (data.inspect[1..-2].gsub('\''){"\\'"})
    result << '\''
    return result
  end
end
hash() click to toggle source

@private

# File lib/erlang/atom.rb, line 136
def hash
  return @internal.hash
end
inspect() click to toggle source

@return [::String] the nicely formatted version of the `Atom`

# File lib/erlang/atom.rb, line 190
def inspect
  if @valid_internal
    return @internal.inspect
  elsif @utf8 == true
    return "Erlang::Atom[#{@data.inspect}, utf8: true]"
  else
    return "Erlang::Atom[#{@data.inspect}]"
  end
end
length() click to toggle source

Returns the length of this `Atom`.

@return [Integer]

# File lib/erlang/atom.rb, line 157
def length
  return @data.bytesize
end
Also aliased as: size
marshal_dump() click to toggle source

@return [::Array] @private

# File lib/erlang/atom.rb, line 228
def marshal_dump
  return [@internal, @utf8]
end
marshal_load(args) click to toggle source

@private

# File lib/erlang/atom.rb, line 233
def marshal_load(args)
  internal, utf8 = args
  atom = self.class[internal, utf8: utf8]
  initialize(atom.data, atom.utf8)
  __send__(:immutable!)
  return self
end
size()
Alias for: length
to_atom() click to toggle source

@return [self] the `Atom` version of the `Atom`

# File lib/erlang/atom.rb, line 201
def to_atom
  return self
end
to_binary() click to toggle source

@return [Binary] the `Binary` version of the `Atom`

# File lib/erlang/atom.rb, line 206
def to_binary
  return Erlang::Binary[@data]
end
to_list() click to toggle source

@return [List] the `List` version of the `Atom`

# File lib/erlang/atom.rb, line 211
def to_list
  return Erlang::List.from_enum(@data.bytes)
end
to_s() click to toggle source

@return [::String] the string version of the `Atom`

# File lib/erlang/atom.rb, line 221
def to_s
  return @data
end
Also aliased as: to_str
to_str()
Alias for: to_s
to_string() click to toggle source

@return [self] the `String` version of the `Atom`

# File lib/erlang/atom.rb, line 216
def to_string
  return Erlang::String[@data]
end