Implement Array#abbrev

Submitted By:
znz

Difficulty:
Medium

Tags:
arrays, hash

Instructions:
Write a method on array to return the set of unique abbreviations (or substrings) for the strings in array.

Code:
class Array
  def abbrev
    ___
  end
end

assert_equal ["ruby"].abbrev, {
    "r"    => "ruby",
    "ru"   => "ruby",
    "rub"  => "ruby",
    "ruby" => "ruby"
}
assert_equal %w[ruby ruby].abbrev, { "ruby" => "ruby" }
assert_equal ['ru by'].abbrev, {
  "r"     => "ru by",
  "ru"    => "ru by",
  "ru "   => "ru by",
  "ru b"  => "ru by",
  "ru by" => "ru by" }
assert_equal %w[ruby rubyist].abbrev, {
  "ruby"    => "ruby",
  "rubyi"   => "rubyist",
  "rubyis"  => "rubyist",
  "rubyist" => "rubyist" }
assert_equal %w{ car cone }.abbrev, {
  "ca" => "car", "car" => "car",
  "co" => "cone", "con" => "cone",
  "cone" => "cone" }

Your Solution

Ruby 1.9.3

Back to Problems