Sha256: ec6af99e609e53e8523ca601f79f3ea896704aca6791e9f5cada5ef50824bfbd

Contents?: true

Size: 1.81 KB

Versions: 80

Compression:

Stored size: 1.81 KB

Contents

# Extending the Symbol class.
#
class Symbol

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

  # Returns a metaphone code for this symbol.
  #
  def metaphone
    code = Text::Metaphone.metaphone self.to_s
    code.intern if code
  end

  # Returns a soundex code for this symbol.
  #
  def soundex
    code = Text::Soundex.soundex self.to_s
    code.intern if code
  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.id2name

    sub = sub[range] if range

    yield sub.intern

    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.chop!.intern }
  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 sym)
  #
  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].intern
      end
      this_many += 1
    end
  end

end

Version data entries

80 entries across 80 versions & 1 rubygems

Version Path
picky-4.12.10 lib/picky/extensions/symbol.rb
picky-4.12.8 lib/picky/extensions/symbol.rb
picky-4.12.7 lib/picky/extensions/symbol.rb
picky-4.12.6 lib/picky/extensions/symbol.rb
picky-4.12.5 lib/picky/extensions/symbol.rb
picky-4.12.4 lib/picky/extensions/symbol.rb
picky-4.12.3 lib/picky/extensions/symbol.rb
picky-4.12.2 lib/picky/extensions/symbol.rb
picky-4.12.1 lib/picky/extensions/symbol.rb
picky-4.12.0 lib/picky/extensions/symbol.rb
picky-4.11.3 lib/picky/extensions/symbol.rb
picky-4.11.2 lib/picky/extensions/symbol.rb
picky-4.11.1 lib/picky/extensions/symbol.rb
picky-4.11.0 lib/picky/extensions/symbol.rb
picky-4.10.0 lib/picky/extensions/symbol.rb
picky-4.9.0 lib/picky/extensions/symbol.rb
picky-4.8.1 lib/picky/extensions/symbol.rb
picky-4.8.0 lib/picky/extensions/symbol.rb
picky-4.7.0 lib/picky/extensions/symbol.rb
picky-4.6.6 lib/picky/extensions/symbol.rb