Open In App

Ruby | Array abbrev() function

Last Updated : 08 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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

Syntax: Array.abbrev()
Parameter:– string for abbreviation 
– pattern [optional]
Return: unambiguous abbreviations set

Example #1 : Example for abbrev() method
 

Ruby




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


Output : 
 

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

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

Example #2 : Example for abbrev() method 
 

Ruby




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


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

 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads