Class: Erlang::Atom
- Inherits:
-
Object
- Object
- Erlang::Atom
- 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 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
Instance Attribute Summary collapse
-
#data ⇒ ::String
readonly
Return the data for this
Atom
. -
#utf8 ⇒ ::Boolean
readonly
Return the utf8 flag for this
Atom
.
Class Method Summary collapse
-
.[](*data, utf8: false) ⇒ Atom
Create a new
Atom
populated with the givendata
andutf8
flag. -
.compare(a, b) ⇒ -1, ...
Compares
a
andb
and returns whether they are less than, equal to, or greater than each other. -
.empty ⇒ Atom
Return an empty
Atom
. -
.false ⇒ Atom
Return a false
Atom
. -
.nil ⇒ Atom
Return a nil
Atom
. -
.true ⇒ Atom
Return a true
Atom
.
Instance Method Summary collapse
-
#eql?(other) ⇒ Boolean
(also: #==)
Return true if
other
has the same type and contents as thisAtom
. -
#erlang_inspect(raw = false) ⇒ ::String
Return the contents of this
Atom
as a Erlang-readable::String
. -
#inspect ⇒ ::String
The nicely formatted version of the
Atom
. -
#length ⇒ Integer
(also: #size)
Returns the length of this
Atom
. -
#to_atom ⇒ self
The
Atom
version of theAtom
. -
#to_binary ⇒ Binary
The
Binary
version of theAtom
. -
#to_list ⇒ List
The
List
version of theAtom
. -
#to_s ⇒ ::String
(also: #to_str)
The string version of the
Atom
. -
#to_string ⇒ self
The
String
version of theAtom
.
Instance Attribute Details
#data ⇒ ::String (readonly)
Return the data for this Atom
28 29 30 |
# File 'lib/erlang/atom.rb', line 28 def data @data end |
#utf8 ⇒ ::Boolean (readonly)
Return the utf8 flag for this Atom
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.
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.
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 |
.empty ⇒ Atom
Return an empty Atom
. If used on a subclass, returns an empty instance
of that class.
66 67 68 |
# File 'lib/erlang/atom.rb', line 66 def empty return @empty ||= self.new end |
.false ⇒ Atom
Return a false Atom
.
73 74 75 |
# File 'lib/erlang/atom.rb', line 73 def false return @false ||= self.new("false") end |
.nil ⇒ Atom
Return a nil Atom
.
80 81 82 |
# File 'lib/erlang/atom.rb', line 80 def nil return @nil ||= self.new("nil") end |
.true ⇒ Atom
Return a true Atom
.
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
.
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
.
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
.
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 |
#length ⇒ Integer Also known as: size
Returns the length of this Atom
.
157 158 159 |
# File 'lib/erlang/atom.rb', line 157 def length return @data.bytesize end |
#to_atom ⇒ self
Returns the Atom
version of the Atom
.
201 202 203 |
# File 'lib/erlang/atom.rb', line 201 def to_atom return self end |
#to_binary ⇒ Binary
Returns 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_list ⇒ List
Returns 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
.
221 222 223 |
# File 'lib/erlang/atom.rb', line 221 def to_s return @data end |