lib/naturally/segment.rb in naturally-1.3.2 vs lib/naturally/segment.rb in naturally-1.4.0

- old
+ new

@@ -11,18 +11,33 @@ def <=>(other) to_array <=> other.to_array end + # @return [Array] a representation of myself in array form + # which enables me to be compared against + # another instance for sorting. + # The array is prepended with a symbol so + # two arrays are always comparable. + # + # @example a simple number + # Segment.new('10').to_array #=> [:int, 10] + # + # @example a college course code + # Segment.new('MATH101').to_array #=> [:str, "MATH", 101] + # + # @example Section 633a of the ADEA + # Segment.new('633a').to_array #=> [:int, 633, "a"] def to_array + # TODO: Refactor, probably via polymorphism if @val =~ /^(\p{Digit}+)(\p{Alpha}+)$/ - [$1.to_i, $2] + [:int, $1.to_i, $2] elsif @val =~ /^(\p{Alpha}+)(\p{Digit}+)$/ - [$1, $2.to_i] + [:str, $1, $2.to_i] elsif @val =~ /^\p{Digit}+$/ - [@val.to_i] + [:int, @val.to_i] else - [@val] + [:str, @val] end end end end