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
require 'abbrev'
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
require 'abbrev'
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
Please Login to comment...