红宝石 |缩写函数
Ruby 中的 abbrev函数用于计算 self 中字符串的明确缩写集。
Syntax: %w{ Strings }.abbrev
Here String is the input word whose unambiguous abbreviations are going to be calculated.
Parameters: This function does not accept any parameters.
Returns: the set of all possible unambiguous abbreviations.
示例 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"}