Ruby 数组 abbrev()函数
abbrev() 是一个数组类方法,它为字符串提供明确的缩写集。
语法: Array.abbrev()
返回: 毫不含糊的缩写集
例子#1: abbrev()方法的例子
# Ruby code for abbrev() method
# checking for unambiguous abbreviations
require 'abbrev'
# different pattern style
puts "abbreviation : #{%w{ hello goa }.abbrev}\n\n"
puts "abbreviation : #{%w{ geeks }.abbrev}\n\n"
输出 :
abbreviation : {"hello"=>"hello", "hell"=>"hello", "hel"=>"hello", "he"=>"hello", "h"=>"hello", "goa"=>"goa", "go"=>"goa", "g"=>"goa"}
abbreviation : {"geeks"=>"geeks", "geek"=>"geeks", "gee"=>"geeks", "ge"=>"geeks", "g"=>"geeks"}
例子#2: abbrev()方法的例子
# Ruby code for abbrev() method
# checking for pattern
require 'abbrev'
# checking pattern
puts "pattern : #{%w{ coat past ray }.abbrev(/^.a/)}\n\n"
puts "pattern : #{Abbrev.abbrev(%w{dropping dropper drop}, "drop")}\n\n"
输出 :
pattern : {"past"=>"past", "pas"=>"past", "pa"=>"past", "ray"=>"ray", "ra"=>"ray"}
pattern : {"dropping"=>"dropping", "droppin"=>"dropping", "droppi"=>"dropping", "dropper"=>"dropper", "droppe"=>"dropper", "drop"=>"drop"}