Class: Wordify::Token::Hundreds

Inherits:
Object
  • Object
show all
Defined in:
lib/wordify/token/hundreds.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Hundreds

Returns a new instance of Hundreds.



3
4
5
# File 'lib/wordify/token/hundreds.rb', line 3

def initialize(string)
  @string, @number = string, string.to_i
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



2
3
4
# File 'lib/wordify/token/hundreds.rb', line 2

def number
  @number
end

#stringObject (readonly)

Returns the value of attribute string.



2
3
4
# File 'lib/wordify/token/hundreds.rb', line 2

def string
  @string
end

Instance Method Details

#<=>(other) ⇒ Object



41
42
43
# File 'lib/wordify/token/hundreds.rb', line 41

def <=>(other)
  to_s <=> other.to_s
end

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

Returns:

  • (Boolean)


36
37
38
# File 'lib/wordify/token/hundreds.rb', line 36

def eql?(other)
  to_s == other.to_s
end

#groupObject



27
28
29
# File 'lib/wordify/token/hundreds.rb', line 27

def group
  @group ||= [hundreds, tens].compact
end

#hundredsObject



19
20
21
# File 'lib/wordify/token/hundreds.rb', line 19

def hundreds
  tokens[0].zero? ? nil : "#{tokens[0].unit} hundred"
end

#raw_tokensObject



7
8
9
# File 'lib/wordify/token/hundreds.rb', line 7

def raw_tokens
  @raw_tokens ||= string.scan(/[0-9]/).map { |t| Wordify::Token::Unit.new(t) }
end

#sizeObject



15
16
17
# File 'lib/wordify/token/hundreds.rb', line 15

def size
  tokens.size
end

#tensObject



23
24
25
# File 'lib/wordify/token/hundreds.rb', line 23

def tens
  Wordify::Token::Tens.new(tokens[1], tokens[2]).to_s
end

#to_sObject



31
32
33
34
# File 'lib/wordify/token/hundreds.rb', line 31

def to_s
  return nil if number.zero?
  group.any? ? group.join(' and ') : nil
end

#tokensObject



11
12
13
# File 'lib/wordify/token/hundreds.rb', line 11

def tokens
  @tokens ||= ([Wordify::Token::Unit.new('0')]*(3-raw_tokens.size)) + raw_tokens
end