Class: Erlang::Atom

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

Overview

An Atom is a literal constant with a name.

Symbols, Booleans (true and false), and nil are considered Atoms 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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#data::String (readonly)

Return the data for this Atom

Returns:

  • (::String)


28
29
30
# File 'lib/erlang/atom.rb', line 28

def data
  @data
end

#utf8::Boolean (readonly)

Return the utf8 flag for this Atom

Returns:

  • (::Boolean)


32
33
34
# File 'lib/erlang/atom.rb', line 32

def utf8
  @utf8
end

Class Method Details

.[](*data, utf8: false) ⇒ Atom

Create a new Atom populated with the given data and utf8 flag.

Parameters:

  • data (::String, Symbol, ::Enumerable, Integer)

    The content of the Atom

  • utf8 (Boolean) (defaults to: false)

    Whether the Atom should be considered UTF-8 or not

Returns:

Raises:

  • (ArgumentError)

    if data cannot be coerced to be a ::String



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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) ⇒ -1, ...

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

Parameters:

  • a (Atom)

    The left argument

  • b (Atom)

    The right argument

Returns:

  • (-1, 0, 1)

Raises:

  • (ArgumentError)

    if a or b is not an Atom



98
99
100
101
102
# 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

.emptyAtom

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

Returns:



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

def empty
  return @empty ||= self.new
end

.falseAtom

Return a false Atom.

Returns:



73
74
75
# File 'lib/erlang/atom.rb', line 73

def false
  return @false ||= self.new("false")
end

.nilAtom

Return a nil Atom.

Returns:



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

def nil
  return @nil ||= self.new("nil")
end

.trueAtom

Return a true Atom.

Returns:



87
88
89
# File 'lib/erlang/atom.rb', line 87

def true
  return @true ||= self.new("true")
end

Instance Method Details

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

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

Parameters:

  • other (Object)

    The object to compare with

Returns:

  • (Boolean)


144
145
146
147
148
149
150
151
# 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

#erlang_inspect(raw = false) ⇒ ::String

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

Examples:

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
# => "'Ω'"

Returns:

  • (::String)


174
175
176
177
178
179
180
181
182
183
184
185
186
187
# 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

#inspect::String

Returns the nicely formatted version of the Atom.

Returns:

  • (::String)

    the nicely formatted version of the Atom



190
191
192
193
194
195
196
197
198
# 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

#lengthInteger Also known as: size

Returns the length of this Atom.

Returns:

  • (Integer)


157
158
159
# File 'lib/erlang/atom.rb', line 157

def length
  return @data.bytesize
end

#to_atomself

Returns the Atom version of the Atom.

Returns:

  • (self)

    the Atom version of the Atom



201
202
203
# File 'lib/erlang/atom.rb', line 201

def to_atom
  return self
end

#to_binaryBinary

Returns the Binary version of the Atom.

Returns:

  • (Binary)

    the Binary version of the Atom



206
207
208
# File 'lib/erlang/atom.rb', line 206

def to_binary
  return Erlang::Binary[@data]
end

#to_listList

Returns the List version of the Atom.

Returns:

  • (List)

    the List version of the Atom



211
212
213
# File 'lib/erlang/atom.rb', line 211

def to_list
  return Erlang::List.from_enum(@data.bytes)
end

#to_s::String Also known as: to_str

Returns the string version of the Atom.

Returns:

  • (::String)

    the string version of the Atom



221
222
223
# File 'lib/erlang/atom.rb', line 221

def to_s
  return @data
end

#to_stringself

Returns the String version of the Atom.

Returns:

  • (self)

    the String version of the Atom



216
217
218
# File 'lib/erlang/atom.rb', line 216

def to_string
  return Erlang::String[@data]
end