Sha256: 306734458d4c73fd407a23487b10ff72e90a909e3e06f355523dde2aedb4ca85

Contents?: true

Size: 1.74 KB

Versions: 80

Compression:

Stored size: 1.74 KB

Contents

# Extending the String class.
#
class String

  # Returns a _single_ double metaphone code
  # for this string.
  #
  def double_metaphone
    codes = Text::Metaphone.double_metaphone self
    codes.first unless codes.empty?
  end

  # Returns a metaphone code for this string.
  #
  def metaphone
    Text::Metaphone.metaphone self
  end

  # Returns a soundex code for this string.
  #
  def soundex
    Text::Soundex.soundex self
  end

  # 'keys'.each_subtoken    # => yields each of ['keys', 'key', 'ke', 'k']
  # 'keys'.each_subtoken(2) # => yields each of ['keys', 'key', 'ke']
  #
  def each_subtoken from_length = 1, range = nil
    sub = self

    sub = sub[range] if range

    yield sub

    size = sub.size
    from_length = size + from_length + 1 if from_length < 0
    from_length = size if size < from_length
    from_length = 1 if from_length < 1

    size.downto(from_length + 1) { yield sub = sub.chop }
  end

  # 'keys'.each_intoken         # => yields each of ['keys', 'key', 'eys', 'ke', 'ey', 'ys', 'k', 'e', 'y', 's']
  # 'keys'.each_intoken(2)      # => yields each of ['keys', 'key', 'eys', 'ke', 'ey', 'ys']
  # 'keys'.each_intoken(2, 3)   # => yields each of ['key', 'eys', 'ke', 'ey', 'ys']
  # 'keys'.each_intoken(10, 12) # => yields nothing (min larger than str)
  #
  def each_intoken min_length = 1, max_length = -1
    max_length = size + max_length + 1 if max_length < 0
    max_length = size if size < max_length
    max_length = 1 if max_length < 1

    min_length = size + min_length + 1 if min_length < 0
    min_length = 1 if min_length < 1

    this_many = size - max_length + 1
    max_length.downto(min_length) do |length|
      this_many.times do |offset|
        yield self[offset, length]
      end
      this_many += 1
    end
  end

end

Version data entries

80 entries across 80 versions & 1 rubygems

Version Path
picky-4.19.7 lib/picky/extensions/string.rb
picky-4.19.6 lib/picky/extensions/string.rb
picky-4.19.5 lib/picky/extensions/string.rb
picky-4.19.4 lib/picky/extensions/string.rb
picky-4.19.3 lib/picky/extensions/string.rb
picky-4.19.2 lib/picky/extensions/string.rb
picky-4.19.1 lib/picky/extensions/string.rb
picky-4.19.0 lib/picky/extensions/string.rb
picky-4.18.0 lib/picky/extensions/string.rb
picky-4.17.1 lib/picky/extensions/string.rb
picky-4.17.0 lib/picky/extensions/string.rb
picky-4.16.0 lib/picky/extensions/string.rb
picky-4.15.1 lib/picky/extensions/string.rb
picky-4.15.0 lib/picky/extensions/string.rb
picky-4.14.0 lib/picky/extensions/string.rb
picky-4.13.1 lib/picky/extensions/string.rb
picky-4.13.0 lib/picky/extensions/string.rb
picky-4.12.13 lib/picky/extensions/string.rb
picky-4.12.12 lib/picky/extensions/string.rb
picky-4.12.11 lib/picky/extensions/string.rb