📜  红宝石 |缩写函数

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

红宝石 |缩写函数

Ruby 中的 abbrev函数用于计算 self 中字符串的明确缩写集。

示例 1:

# Ruby Program of abbrev function
require 'abbrev'
   
# Calling the abbrev function with string
A = %w{ Geek }.abbrev
B = %w{ gfg }.abbrev
   
# Getting the set of unambiguous 
# abbreviations for the string in self.
puts "#{A}"
puts "#{B}"

输出:

{"Geek"=>"Geek", "Gee"=>"Geek", "Ge"=>"Geek", "G"=>"Geek"}
{"gfg"=>"gfg", "gf"=>"gfg", "g"=>"gfg"}

示例 2:

# Ruby Program of abbrev function
require 'abbrev'
   
# Calling the abbrev function with some strings
A = %w{ Geek CSE }.abbrev
B = %w{ gfg  GFG }.abbrev
   
# Getting the set of unambiguous 
# abbreviations for the strings in self.
puts "#{A}"
puts "#{B}"

输出:

{"Geek"=>"Geek", "Gee"=>"Geek", "Ge"=>"Geek", "G"=>"Geek", "CSE"=>"CSE", "CS"=>"CSE", "C"=>"CSE"}
{"gfg"=>"gfg", "gf"=>"gfg", "g"=>"gfg", "GFG"=>"GFG", "GF"=>"GFG", "G"=>"GFG"}