Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Ruby | abbrev function

  • Last Updated : 25 Oct, 2019

The abbrev function in Ruby is used to calculates the set of unambiguous abbreviations for the strings in 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.

Example 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}"

Output:

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

Example 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}"

Output:

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!