Class: NaturalSort::Segment

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/natural_sort/segment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Segment

Returns a new instance of Segment.



12
13
14
# File 'lib/natural_sort/segment.rb', line 12

def initialize(input)
  @input = input.to_s
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



10
11
12
# File 'lib/natural_sort/segment.rb', line 10

def input
  @input
end

Instance Method Details

#<=>(other) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/natural_sort/segment.rb', line 20

def <=>(other)
  if numeric? && other.numeric?
    Rational(input) <=> Rational(other.to_s)
  else
    compare_chars(input, other.to_s)
  end
end

#numeric?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/natural_sort/segment.rb', line 28

def numeric?
  NUMERIC === input
end

#to_sObject



16
17
18
# File 'lib/natural_sort/segment.rb', line 16

def to_s
  @input
end