Ruby | Array abbrev() function

abbrev() is an Array class method which provides unambiguous set of abbreviations for the string.

Syntax: Array.abbrev()

Parameter:– string for abbrevation
– pattern [optional]

Return: unambiguous abbreviations set

Example #1 : Example for abbrev() method



filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for abbrev() method
# checking for unambiguous abbreviations
  
require 'abbrev'
  
# different pattern style
puts "abbrevation : #{%w{ hello goa }.abbrev}\n\n"
  
puts "abbrevation : #{%w{ geeks }.abbrev}\n\n"

chevron_right


Output :

abbrevation : {"hello"=>"hello", "hell"=>"hello", "hel"=>"hello", "he"=>"hello", "h"=>"hello", "goa"=>"goa", "go"=>"goa", "g"=>"goa"}

abbrevation : {"geeks"=>"geeks", "geek"=>"geeks", "gee"=>"geeks", "ge"=>"geeks", "g"=>"geeks"}

Example #2 : Example for abbrev() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for abbrev() method
# checking for pattern
  
require 'abbrev'
  
# checking pattern
puts "pattern : #{%w{ coat past ray }.abbrev(/^.a/)}\n\n"
  
puts "pattern : #{Abbrev.abbrev(%w{dropping dropper drop}, "drop")}\n\n"

chevron_right


Output :

pattern : {"past"=>"past", "pas"=>"past", "pa"=>"past", "ray"=>"ray", "ra"=>"ray"}

pattern : {"dropping"=>"dropping", "droppin"=>"dropping", "droppi"=>"dropping", "dropper"=>"dropper", "droppe"=>"dropper", "drop"=>"drop"}
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.