当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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模块。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。