📜  红宝石 |数组 abbrev()函数

📅  最后修改于: 2022-05-13 01:54:52.141000             🧑  作者: Mango

红宝石 |数组 abbrev()函数

abbrev()是一个 Array 类方法,它为字符串提供明确的缩写集。

示例 #1: abbrev() 方法示例

Ruby
# 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"


Ruby
# 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"


输出 :

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"}

Example #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"}