diff options
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | lib/abbrev.rb | 47 |
2 files changed, 43 insertions, 9 deletions
@@ -1,5 +1,10 @@ Sun Oct 7 04:50:00 2012 Zachary Scott <zzak@ruby-lang.org> + * lib/abbrev.rb: Documentation examples for Abbrev. + [ruby-dev:47442] [Bug #6985] + +Sun Oct 7 04:50:00 2012 Zachary Scott <zzak@ruby-lang.org> + * thread.c (rb_thread_aref): Grammar in Thread documentation. Patch by Steve Klabnik [ruby-dev:47799] [Bug #7099] 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 |