### Home Classes Methods

### In Files

- lib/chronic/numerizer.rb

### Parent

Object

### Methods

- ::numerize

### Files

- LICENSE

### Class/Module Index

Quicksearch

- Chronic
- Chronic::Date
- Chronic::Grabber
- Chronic::Handler
- Chronic::Handlers
- Chronic::MiniDate
- Chronic::Numerizer
- Chronic::Ordinal
- Chronic::Parser
- Chronic::Pointer
- Chronic::Repeater
- Chronic::Scalar
- Chronic::Season
- Chronic::Separator
- Chronic::Sign
- Chronic::Span
- Chronic::Tag
- Chronic::Time
- Chronic::TimeZone
- Chronic::Token

No matching classes.

# Chronic::Numerizer

### Constants

BIG_PREFIXES
DIRECT_NUMS
ORDINALS
TEN_PREFIXES

### Public Class Methods

numerize(string) click to toggle source

    # File lib/chronic/numerizer.rb, line 72
    def self.numerize(string)
      string = string.dup

      # preprocess
      string.gsub!(/ +|([^\d])-([^\d])/, '\1 \2') # will mutilate hyphenated-words but shouldn't matter for date extraction
      string.gsub!(/a half/, 'haAlf') # take the 'a' out so it doesn't turn into a 1, save the half for the end

      # easy/direct replacements

      DIRECT_NUMS.each do |dn|
        string.gsub!(/#{dn[0]}/, '<num>' + dn[1])
      end

      ORDINALS.each do |on|
        string.gsub!(/#{on[0]}/, '<num>' + on[1] + on[0][-2, 2])
      end

      # ten, twenty, etc.

      TEN_PREFIXES.each do |tp|
        string.gsub!(/(?:#{tp[0]}) *<num>(\d(?=[^\d]|$))*/) { '<num>' + (tp[1] + $1.to_i).to_s }
      end

      TEN_PREFIXES.each do |tp|
        string.gsub!(/#{tp[0]}/) { '<num>' + tp[1].to_s }
      end

      # hundreds, thousands, millions, etc.

      BIG_PREFIXES.each do |bp|
        string.gsub!(/(?:<num>)?(\d*) *#{bp[0]}/) { $1.empty? ? bp[1] : '<num>' + (bp[1] * $1.to_i).to_s}
        andition(string)
      end

      # fractional addition
      # I'm not combining this with the previous block as using float addition complicates the strings
      # (with extraneous .0's and such )
      string.gsub!(/(\d+)(?: | and |-)*haAlf/) { ($1.to_f + 0.5).to_s }

      string.gsub(/<num>/, '')
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.