module Gem::Text
A collection of text-wrangling methods
Public Instance Methods
Source
# File lib/rubygems/text.rb, line 10defclean_text(text)text.gsub(/[\000-\b\v-\f\016-\037\177]/,".")end
Remove any non-printable characters and make the text suitable for printing.
Source
# File lib/rubygems/text.rb, line 24defformat_text(text,wrap,indent =0)result = []work =clean_text(text)whilework.length>wrapdoifwork=~/^(.{0,#{wrap}})[ \n]/result<<$1.rstripwork.slice!(0,$&.length)elseresult<<work.slice!(0,wrap)endendresult<<workifwork.length.nonzero?result.join("\n").gsub(/^/," "*indent)end
Wrapstext towrap characters and optionally indents byindent characters
Source
# File lib/rubygems/text.rb, line 54deflevenshtein_distance(str1,str2)n =str1.lengthm =str2.lengthreturnmifn.zero?returnnifm.zero?d = (0..m).to_ax =nil# to avoid duplicating an enumerable object, create it outside of the loopstr2_codepoints =str2.codepointsstr1.each_codepoint.with_index(1)do|char1,i|j =0whilej<mcost =char1==str2_codepoints[j]?0:1x =min3(d[j+1]+1,# insertioni+1,# deletiond[j]+cost# substitution )d[j] =ii =xj+=1endd[m] =xendxend
Returns a value representing the “cost” of transforming str1 into str2 Vendored version of DidYouMean::Levenshtein.distance from the ruby/did_you_mean gem @ 1.4.0github.com/ruby/did_you_mean/blob/2ddf39b874808685965dbc47d344cf6c7651807c/lib/did_you_mean/levenshtein.rb#L7-L37
Source
# File lib/rubygems/text.rb, line 14deftruncate_text(text,description,max_length =100_000)raiseArgumentError,"max_length must be positive"unlessmax_length>0returntextiftext.size<=max_length"Truncating #{description} to #{max_length.to_s.reverse.gsub(/...(?=.)/,'\&,').reverse} characters:\n"+text[0,max_length]end