Ruby | abbrev function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby Program of abbrev function
require 'abbrev'
   
# Calling the abbrev funtion 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}"

chevron_right


Output:

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby Program of abbrev function
require 'abbrev'
   
# Calling the abbrev funtion 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}"

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.