Class: Erlang::String

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

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#data::String (readonly)

Return the data for this String

Returns:

  • (::String)


20
21
22
# File 'lib/erlang/string.rb', line 20

def data
  @data
end

Class Method Details

.[](*data) ⇒ String

Create a new String populated with the given data.

Parameters:

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

    The content of the Atom

Returns:

Raises:

  • (ArgumentError)

    if data cannot be coerced to be a ::String



27
28
29
30
31
32
33
34
35
36
# 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) ⇒ -1, ...

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

Parameters:

  • a (String)

    The left argument

  • b (String)

    The right argument

Returns:

  • (-1, 0, 1)

Raises:

  • (ArgumentError)

    if a or b is not a String



53
54
55
56
57
58
59
# 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

.emptyString

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

Returns:



42
43
44
# File 'lib/erlang/string.rb', line 42

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

Instance Method Details

#empty?Boolean

Returns true if this String is empty.

Returns:

  • (Boolean)


77
78
79
# File 'lib/erlang/string.rb', line 77

def empty?
  return @data.empty?
end

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

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

Parameters:

  • other (Object)

    The object to compare with

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
# 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

#erlang_inspect(raw = false) ⇒ ::String

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

Examples:

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

Returns:

  • (::String)


120
121
122
123
124
125
126
# 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[::String]

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

Returns:

  • ([::String])


84
85
86
# File 'lib/erlang/string.rb', line 84

def flatten
  return [@data]
end

#inspect::String

Returns the nicely formatted version of the String.

Returns:

  • (::String)

    the nicely formatted version of the String



129
130
131
# File 'lib/erlang/string.rb', line 129

def inspect
  return "Erlang::String[#{@data.inspect}]"
end

#lengthInteger Also known as: size

Returns the length of this String.

Returns:

  • (Integer)


91
92
93
# File 'lib/erlang/string.rb', line 91

def length
  return @data.bytesize
end

#to_atomAtom

Returns the Atom version of the String.

Returns:

  • (Atom)

    the Atom version of the String



134
135
136
# File 'lib/erlang/string.rb', line 134

def to_atom
  return Erlang::Atom[@data]
end

#to_binaryBinary

Returns the Binary version of the String.

Returns:

  • (Binary)

    the Binary version of the String



139
140
141
# File 'lib/erlang/string.rb', line 139

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

#to_listList

Returns the List version of the String.

Returns:

  • (List)

    the List version of the String



144
145
146
# File 'lib/erlang/string.rb', line 144

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 String.

Returns:

  • (::String)

    the string version of the String



154
155
156
# File 'lib/erlang/string.rb', line 154

def to_s
  return @data
end

#to_stringself

Returns the String version of the String.

Returns:

  • (self)

    the String version of the String



149
150
151
# File 'lib/erlang/string.rb', line 149

def to_string
  return self
end