lib/picky/extensions/symbol.rb in picky-0.0.9 vs lib/picky/extensions/symbol.rb in picky-0.1.0
- old
+ new
@@ -1,18 +1,33 @@
# Extending the Symbol class.
#
class Symbol
- # :keys.subtokens # => [:key, :ke, :k]
- # :keys.subtokens(2) # => [:key, :ke]
+ # :keys.subtokens # => [:keys, :key, :ke, :k]
+ # :keys.subtokens(2) # => [:keys, :key, :ke]
#
def subtokens down_to_length = 1
- sub, result = self.to_s, [self]
+ sub = self.id2name
size = sub.size
+ down_to_length = size + down_to_length if down_to_length < 0
down_to_length = size if size < down_to_length
- size.downto(down_to_length + 1) { result << sub.chop!.to_sym }
+ result = [self]
+ size.downto(down_to_length + 1) { result << sub.chop!.intern }
result
+ end
+
+ # TODO Duplicate code.
+ #
+ def each_subtoken down_to_length = 1
+ sub = self.id2name
+
+ size = sub.size
+ down_to_length = size + down_to_length if down_to_length < 0
+ down_to_length = size if size < down_to_length
+
+ yield self
+ size.downto(down_to_length + 1) { yield sub.chop!.intern }
end
end
\ No newline at end of file