Class: OrdinalParser

Inherits:
Object
  • Object
show all
Defined in:
lib/ordinal_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(string, password = nil) ⇒ OrdinalParser

Returns a new instance of OrdinalParser.



2
3
4
5
# File 'lib/ordinal_parser.rb', line 2

def initialize(string, password=nil)
  @input = string
  @password = password
end

Instance Method Details

#ordinalObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ordinal_parser.rb', line 7

def ordinal
  case @input
    when /the last/
      @password.length
    when /second (from|to) last/
      @password.length - 1
    when /third from last/
      @password.length - 2
    when /fourth from last/
      @password.length - 3
    when /first/
      1
    when /second/
      2
    when /third/
      3
    when /fourth/
      4
    else
      @input[/\d+/].to_i
  end
end