Class: NaturallyUnicode::NumberElement
- Inherits:
-
Object
- Object
- NaturallyUnicode::NumberElement
- Includes:
- Comparable
- Defined in:
- lib/naturally-unicode.rb
Overview
An entity which can be compared to other like elements for sorting in an array. It’s an object representing a value which implements the Comparable interface.
Instance Attribute Summary collapse
-
#val ⇒ Object
Returns the value of attribute val.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#initialize(v) ⇒ NumberElement
constructor
A new instance of NumberElement.
- #letters_with_numbers? ⇒ Boolean
- #numbers_with_letters? ⇒ Boolean
- #pure_integer? ⇒ Boolean
- #reverse_simple_normalize(n) ⇒ Object
- #simple_normalize(n) ⇒ Object
Constructor Details
#initialize(v) ⇒ NumberElement
Returns a new instance of NumberElement.
31 32 33 |
# File 'lib/naturally-unicode.rb', line 31 def initialize(v) @val = v end |
Instance Attribute Details
#val ⇒ Object
Returns the value of attribute val.
29 30 31 |
# File 'lib/naturally-unicode.rb', line 29 def val @val end |
Instance Method Details
#<=>(other) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/naturally-unicode.rb', line 35 def <=>(other) if pure_integer? && other.pure_integer? @val.to_i <=> other.val.to_i elsif numbers_with_letters? || other.numbers_with_letters? simple_normalize(@val) <=> simple_normalize(other.val) elsif letters_with_numbers? || other.letters_with_numbers? reverse_simple_normalize(@val) <=> reverse_simple_normalize(other.val) else @val <=> other.val end end |
#letters_with_numbers? ⇒ Boolean
55 56 57 |
# File 'lib/naturally-unicode.rb', line 55 def letters_with_numbers? val =~ /^\p{Alpha}+\d+$/ end |
#numbers_with_letters? ⇒ Boolean
51 52 53 |
# File 'lib/naturally-unicode.rb', line 51 def numbers_with_letters? val =~ /^\d+\p{Alpha}+$/ end |
#pure_integer? ⇒ Boolean
47 48 49 |
# File 'lib/naturally-unicode.rb', line 47 def pure_integer? @val =~ /^\d+$/ end |
#reverse_simple_normalize(n) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/naturally-unicode.rb', line 67 def reverse_simple_normalize(n) if n =~ /^(\p{Alpha}+)(\d+)$/ [$1, $2.to_i] else [n.to_s] end end |
#simple_normalize(n) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/naturally-unicode.rb', line 59 def simple_normalize(n) if n =~ /^(\d+)(\p{Alpha}+)$/ [$1.to_i, $2] else [n.to_i] end end |