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


Ruby Abbrev模塊用法及代碼示例


本文簡要介紹ruby語言中 Abbrev模塊 的用法。

計算給定字符串集的明確縮寫集。

require 'abbrev'
require 'pp'

pp Abbrev.abbrev(['ruby'])
#=>  {"ruby"=>"ruby", "rub"=>"ruby", "ru"=>"ruby", "r"=>"ruby"}

pp Abbrev.abbrev(%w{ ruby rules })

Generates:

{ "ruby"  =>  "ruby",
  "rub"   =>  "ruby",
  "rules" =>  "rules",
  "rule"  =>  "rules",
  "rul"   =>  "rules" }

它還提供了一個陣列核心擴展 Array#abbrev

pp %w{ summer winter }.abbrev

Generates:

{ "summer"  => "summer",
  "summe"   => "summer",
  "summ"    => "summer",
  "sum"     => "summer",
  "su"      => "summer",
  "s"       => "summer",
  "winter"  => "winter",
  "winte"   => "winter",
  "wint"    => "winter",
  "win"     => "winter",
  "wi"      => "winter",
  "w"       => "winter" }

相關用法


注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Abbrev模塊。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。