Open In App

Ruby | abbrev function

Last Updated : 25 Oct, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads