當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Ruby Array abbrev()用法及代碼示例


abbrev()是一個Array類方法,可為字符串提供明確的縮寫集。

用法:Array.abbrev()

參數:–縮寫字符串
–模式[可選]


返回:明確的縮寫集

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

# Ruby code for abbrev() method 
# checking for unambiguous abbreviations 
  
require 'abbrev'
  
# different pattern style 
puts "abbrevation : #{%w{ hello goa }.abbrev}\n\n"
  
puts "abbrevation : #{%w{ geeks }.abbrev}\n\n"

輸出:

abbrevation : {"hello"=>"hello", "hell"=>"hello", "hel"=>"hello", "he"=>"hello", "h"=>"hello", "goa"=>"goa", "go"=>"goa", "g"=>"goa"}

abbrevation : {"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"}


相關用法


注:本文由純淨天空篩選整理自mayank5326大神的英文原創作品 Ruby | Array abbrev() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。