diff options
author | zzak <zzak@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-10-06 20:03:26 +0000 |
---|---|---|
committer | zzak <zzak@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-10-06 20:03:26 +0000 |
commit | 511703c770acd5b68eb1cbe644f431d9c2ba9564 (patch) | |
tree | b4dc68ebd5e56cea7568a9cf6e55e6de1ffce945 /lib | |
parent | 61d460e5e18d9cebf13157e95d55f3cf1b139205 (diff) | |
download | ruby-511703c770acd5b68eb1cbe644f431d9c2ba9564.tar.gz |
* lib/abbrev.rb: Documentation examples for Abbrev.
[ruby-dev:47442] [Bug #6985]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37113 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r-- | lib/abbrev.rb | 47 |
1 files changed, 38 insertions, 9 deletions
diff --git a/lib/abbrev.rb b/lib/abbrev.rb index aac953aa82..b4b12b88b5 100644 --- a/lib/abbrev.rb +++ b/lib/abbrev.rb @@ -26,21 +26,45 @@ # "rule" => "rules", # "rules" => "rules" } # -# It also adds an +abbrev+ method to class Array. +# It also provides an array core extension, Array#abbrev. +# +# pp %w{april may}.abbrev +# #=> {"summe"=>"summer", +# "summ"=>"summer", +# "sum"=>"summer", +# "su"=>"summer", +# "s"=>"summer", +# "winte"=>"winter", +# "wint"=>"winter", +# "win"=>"winter", +# "wi"=>"winter", +# "w"=>"winter", +# "summer"=>"summer", +# "winter"=>"winter"} module Abbrev # Given a set of strings, calculate the set of unambiguous # abbreviations for those strings, and return a hash where the keys # are all the possible abbreviations and the values are the full - # strings. Thus, given input of "car" and "cone", the keys pointing - # to "car" would be "ca" and "car", while those pointing to "cone" - # would be "co", "con", and "cone". + # strings. + # + # Thus, given input of "car" and "cone", the keys pointing to "car" would be + # "ca" and "car", while those pointing to "cone" would be "co", "con", and + # "cone". + # + # require 'abbrev' + # require 'pp' + # + # pp Abbrev.abbrev(['car', 'cone']) + # #=> {"ca"=>"car", "con"=>"cone", "co"=>"cone", "car"=>"car", "cone"=>"cone"} # # The optional +pattern+ parameter is a pattern or a string. Only # input strings that match the pattern or start with the string # are included in the output hash. - + # + # pp %w{car box cone}.abbrev(/b/) + # #=> {"bo"=>"box", "b"=>"box", "box"=>"box"} def abbrev(words, pattern = nil) table = {} seen = Hash.new(0) @@ -83,15 +107,20 @@ class Array # Calculates the set of unambiguous abbreviations for the strings in # +self+. # + # abbr = %w{ car cone }.abbrev + # abbr #=> { "ca" => "car", "car" => "car", + # "co" => "cone", "con" => "cone", + # "cone" => "cone" } + # # The optional +pattern+ parameter is a pattern or a string. Only # input strings that match the pattern or start with the string # are included in the output hash. # - # %w{ car cone }.abbrev #=> { "ca" => "car", "car" => "car", - # "co" => "cone", "con" => "cone", - # "cone" => "cone" } + # abbr = %w{ fast boat day }.abbrev(/^.a.*$/) + # abbr #=> {"fas"=>"fast","fa"=>"fast", + # "da"=>"day", "fast"=>"fast", "day"=>"day"} # - # See also Abbrev#abbrev + # See also Abbrev.abbrev def abbrev(pattern = nil) Abbrev::abbrev(self, pattern) end |