Skip to content
Related Articles

Related Articles

Ruby | Array abbrev() function
  • Last Updated : 08 Jan, 2020

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






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

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




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