Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Ruby | Array abbrev() function

  • Last Updated : 08 Jul, 2021

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 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 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
Recommended Articles
Page :

Start Your Coding Journey Now!