Sha256: 1ee799818b50e2b7aea1363943477a8263d81535baa68e7de7ab3838de44f439

Contents?: true

Size: 1.3 KB

Versions: 14

Compression:

Stored size: 1.3 KB

Contents

# Extending the Symbol class.
#
class Symbol # :nodoc:all

  # :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
    sub = self.id2name

    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

    yield self
    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 [:keys, :key, :eys]
  # :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

14 entries across 14 versions & 1 rubygems

Version Path
picky-3.2.0 lib/picky/extensions/symbol.rb
picky-3.1.13 lib/picky/extensions/symbol.rb
picky-3.1.12 lib/picky/extensions/symbol.rb
picky-3.1.11 lib/picky/extensions/symbol.rb
picky-3.1.10 lib/picky/extensions/symbol.rb
picky-3.1.9 lib/picky/extensions/symbol.rb
picky-3.1.8 lib/picky/extensions/symbol.rb
picky-3.1.7 lib/picky/extensions/symbol.rb
picky-3.1.6 lib/picky/extensions/symbol.rb
picky-3.1.5 lib/picky/extensions/symbol.rb
picky-3.1.4 lib/picky/extensions/symbol.rb
picky-3.1.3 lib/picky/extensions/symbol.rb
picky-3.1.2 lib/picky/extensions/symbol.rb
picky-3.1.1 lib/picky/extensions/symbol.rb