class Erlang::String

A `String` is a `List` of characters.

### Creating Strings

Erlang::String["test"]
# => Erlang::String["test"]

A `String` is equivalent to a `List` with `Integer` elements:

Erlang::String["test"] == Erlang::List[116, 101, 115, 116]
# => true

Attributes

data[R]

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

Public Class Methods

[](*data) click to toggle source

Create a new `String` populated with the given `data`. @param data [::String, Symbol, ::Enumerable, Integer] The content of the `Atom` @return [String] @raise [ArgumentError] if `data` cannot be coerced to be a `::String`

# File lib/erlang/string.rb, line 27
def [](*data)
  return EmptyString if data.empty?
  if data.size == 1
    return data[0] if data[0].kind_of?(Erlang::String)
  end
  unless data.is_a?(::String)
    data = Erlang.iolist_to_binary(data).data
  end
  return new(data)
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 [String] The left argument @param b [String] The right argument @return [-1, 0, 1] @raise [ArgumentError] if `a` or `b` is not a `String`

# File lib/erlang/string.rb, line 53
def compare(a, b)
  raise ArgumentError, "'a' must be of Erlang::String type" if not a.kind_of?(Erlang::String)
  raise ArgumentError, "'b' must be of Erlang::String type" if not b.kind_of?(Erlang::String)
  c = a.size <=> b.size
  return a.data <=> b.data if c == 0
  return Erlang::List.compare(a.to_list, b.to_list)
end
empty() click to toggle source

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

@return [String]

# File lib/erlang/string.rb, line 42
def empty
  return @empty ||= self.new
end
new(data = ::String.new.freeze) click to toggle source

@private

# File lib/erlang/string.rb, line 63
def initialize(data = ::String.new.freeze)
  raise ArgumentError, 'data must be a String' if not data.is_a?(::String)
  data = Erlang::Terms.binary_encoding(data)
  @data = data.freeze
end

Public Instance Methods

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

Returns true if this `String` is empty.

@return [Boolean]

# File lib/erlang/string.rb, line 77
def empty?
  return @data.empty?
end
eql?(other) click to toggle source

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

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

# File lib/erlang/string.rb, line 100
def eql?(other)
  return true if other.equal?(self)
  if instance_of?(other.class)
    return !!(self.class.compare(self, other) == 0)
  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 `String` as a Erlang-readable `::String`.

@example

Erlang::String["test"].erlang_inspect
# => "\"test\""
# Pass `raw` as `true` for the List version
Erlang::String["test"].erlang_inspect(true)
# => "[116,101,115,116]"

@return [::String]

# File lib/erlang/string.rb, line 120
def erlang_inspect(raw = false)
  if raw == false
    return @data.inspect
  else
    return '[' << @data.bytes.join(',') << ']'
  end
end
flatten() click to toggle source

Returns an `::Array` with the `::String` data for this `String`.

@return [[::String]]

# File lib/erlang/string.rb, line 84
def flatten
  return [@data]
end
hash() click to toggle source

@private

# File lib/erlang/string.rb, line 70
def hash
  return to_list.hash
end
inspect() click to toggle source

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

# File lib/erlang/string.rb, line 129
def inspect
  return "Erlang::String[#{@data.inspect}]"
end
length() click to toggle source

Returns the length of this `String`.

@return [Integer]

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

@return [::String] @private

# File lib/erlang/string.rb, line 161
def marshal_dump
  return @data
end
marshal_load(data) click to toggle source

@private

# File lib/erlang/string.rb, line 166
def marshal_load(data)
  initialize(data)
  __send__(:immutable!)
  return self
end
size()
Alias for: length
to_atom() click to toggle source

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

# File lib/erlang/string.rb, line 134
def to_atom
  return Erlang::Atom[@data]
end
to_binary() click to toggle source

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

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

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

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

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

# File lib/erlang/string.rb, line 154
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 `String`

# File lib/erlang/string.rb, line 149
def to_string
  return self
end