Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Ruby | Array abbrev() function

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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

 

My Personal Notes arrow_drop_up
Last Updated : 08 Jul, 2021
Like Article
Save Article
Similar Reads