Class: Dux::IndifferentString

Inherits:
String
  • Object
show all
Defined in:
lib/dux/indifferent_string.rb

Overview

A string value that is programmatically equivalent to its symbol representation.

Instance Method Summary collapse

Constructor Details

#initialize(stringish) ⇒ IndifferentString

Returns a new instance of IndifferentString.

Parameters:



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/dux/indifferent_string.rb', line 5

def initialize(stringish)
  stringified =
    case stringish
    when String, Dux::IndifferentString then stringish
    when Symbol then stringish.to_s
    when Dux[:to_str] then stringish.to_str
    else
      if Dux.attempt(stringish, :acts_like?, :string)
        stringish.to_s
      else
        raise TypeError, "Not a string or symbol: #{stringish}"
      end
    end

  super(stringified)
end

Instance Method Details

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

Test basic equality.

Parameters:

  • other (String, Symbol)


25
26
27
28
29
30
31
# File 'lib/dux/indifferent_string.rb', line 25

def ==(other)
  if other.kind_of?(Symbol)
    self == other.to_s
  else
    super
  end
end

#===(other) ⇒ Object

Test case equality

Parameters:

  • other (String, Symbol, Regexp)


38
39
40
41
42
43
44
# File 'lib/dux/indifferent_string.rb', line 38

def ===(other)
  if other.kind_of?(Symbol)
    self == other.to_s
  else
    super
  end
end