Sha256: 17ca4c5b8c4b19359217a7ae464af413f1dafb9869ecf4a3c0eba01cb2668bd7

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

# encoding: utf-8

module Okei

  # Breaks the source string to words properly:
  #
  #   Words.new "М3 96% ЧИСТОГО СПИРТА /ЧЕЛ"
  #   # => ["М", "^", "3", "96", "%", "ЧИСТОГО", "СПИРТА", "/", "ЧЕЛ"]
  #
  class Words < Array

    def initialize(line = "")
      @line = line.to_s
      super @line.scan(/[А-Я]+|\d+.\d+|\d+|\/|\^|\%/)
    end

    # Maps words back to `Words` (not a base `Array`):
    #
    #   ["9.8", "М", "/", "С", "^", "2"].map { |i| i }.class
    #   # => Words
    #
    # The method can be combined with `to_s`:
    #
    #   ["9.8", "М", "/", "С", "^", "2"].map { |i| i.lower }.to_s
    #   # => "9.8 м/с2"
    #
    def map
      Words.new super
    end

    # Joins words back to string when necessary:
    #
    #   ["М", "^", "3", "96", "ПРОЦ", "ЧИСТОГО", "СПИРТА", "/", "ЧЕЛ"].to_s
    #   # => "М3 96 ПРОЦ ЧИСТОГО СПИРТА/ЧЕЛ"
    #
    def to_s
      join(" ").gsub(" ^ ", "").gsub(" / ", "/")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
okei-0.0.2 app/models/okei/words.rb