Class: EncodeM::String

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/encode_m/string.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ String

Returns a new instance of String.



8
9
10
11
# File 'lib/encode_m/string.rb', line 8

def initialize(value)
  @value = value.to_s
  @encoded = encode_string(@value)
end

Instance Attribute Details

#encodedObject (readonly)

Returns the value of attribute encoded.



6
7
8
# File 'lib/encode_m/string.rb', line 6

def encoded
  @encoded
end

#valueObject (readonly)

Returns the value of attribute value.



6
7
8
# File 'lib/encode_m/string.rb', line 6

def value
  @value
end

Instance Method Details

#<=>(other) ⇒ Object

Comparison operations



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/encode_m/string.rb', line 35

def <=>(other)
  case other
  when EncodeM::String
    @encoded <=> other.encoded
  when EncodeM::Numeric
    1  # Strings always sort after numbers in M language
  when EncodeM::Composite
    # Let Composite handle the comparison
    -(other <=> self)
  else
    nil
  end
end

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



49
50
51
52
53
54
55
56
57
58
# File 'lib/encode_m/string.rb', line 49

def ==(other)
  case other
  when EncodeM::String
    @value == other.value
  when ::String
    @value == other
  else
    false
  end
end

#empty?Boolean

String-specific predicates

Returns:

  • (Boolean)


26
27
28
# File 'lib/encode_m/string.rb', line 26

def empty?
  @value.empty?
end

#hashObject



62
63
64
# File 'lib/encode_m/string.rb', line 62

def hash
  @value.hash
end

#inspectObject



21
22
23
# File 'lib/encode_m/string.rb', line 21

def inspect
  "EncodeM::String(#{@value.inspect})"
end

#lengthObject



30
31
32
# File 'lib/encode_m/string.rb', line 30

def length
  @value.length
end

#to_encodedObject



17
18
19
# File 'lib/encode_m/string.rb', line 17

def to_encoded
  @encoded
end

#to_sObject



13
14
15
# File 'lib/encode_m/string.rb', line 13

def to_s
  @value
end