Class: String

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

Overview

class NilClass

def to_date
  return nil
end unless method_defined?(:to_date)

end

Instance Method Summary collapse

Instance Method Details

#is_ordinal?Boolean

returns true if the sending string is a text or numeric ordinal (e.g. first or 1st)

Returns:

  • (Boolean)


173
174
175
176
177
# File 'lib/tickle.rb', line 173

def is_ordinal?
  scanner = %w{first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth thirteenth fourteenth fifteenth sixteenth seventeenth eighteenth nineteenth twenty thirty thirtieth}
  regex = /\b(\d*)(st|nd|rd|th)\b/
  !(self =~ regex).nil? || scanner.include?(self.downcase)
end

#ordinal_as_numberObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/tickle.rb', line 179

def ordinal_as_number
  return self unless self.is_ordinal?
  scanner = {/first/ => '1st',
    /second/ => '2nd',
    /third/ => '3rd',
    /fourth/ => '4th',
    /fifth/ => '5th',
    /sixth/ => '6th',
    /seventh/ => '7th',
    /eighth/ => '8th',
    /ninth/ => '9th',
    /tenth/ => '10th',
    /eleventh/ => '11th',
    /twelfth/ => '12th',
    /thirteenth/ => '13th',
    /fourteenth/ => '14th',
    /fifteenth/ => '15th',
    /sixteenth/ => '16th',
    /seventeenth/ => '17th',
    /eighteenth/ => '18th',
    /nineteenth/ => '19th',
    /twentieth/ => '20th',
    /thirtieth/ => '30th',
  }
  result = self
  scanner.keys.each {|scanner_item| result = scanner[scanner_item] if scanner_item =~ self}
  return result.gsub(/\b(\d*)(st|nd|rd|th)\b/, '\1')
end