Exercism Ruby - Acronym

juanvqz

Juan Vasquez

Posted on April 3, 2020

Exercism Ruby - Acronym

Alt Text

Convert a phrase to its acronym.

Techies love their TLA (Three Letter Acronyms)!

Help generate some jargon by writing a program that converts a long name like Portable Network Graphics to its acronym (PNG).

Test suite

require 'minitest/autorun'
require_relative 'acronym'

# Common test data version: 1.7.0 cacf1f1
class AcronymTest < Minitest::Test
  def test_basic
    assert_equal "PNG", Acronym.abbreviate('Portable Network Graphics')
  end

  def test_lowercase_words
    assert_equal "ROR", Acronym.abbreviate('Ruby on Rails')
  end

  def test_punctuation
    assert_equal "FIFO", Acronym.abbreviate('First In, First Out')
  end

  def test_all_caps_word
    assert_equal "GIMP", Acronym.abbreviate('GNU Image Manipulation Program')
  end

  def test_punctuation_without_whitespace
    assert_equal "CMOS", Acronym.abbreviate('Complementary metal-oxide semiconductor')
  end

  def test_very_long_abbreviation
    assert_equal "ROTFLSHTMDCOALM", Acronym.abbreviate('Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me')
  end

  def test_consecutive_delimiters
    assert_equal "SIMUFTA", Acronym.abbreviate('Something - I made up from thin air')
  end
end
Enter fullscreen mode Exit fullscreen mode

My answer was:

class Acronym
  def self.abbreviate(phrase)
    phrase.split("-").join(" ").split.map(&:chr).join.upcase
  end
end
Enter fullscreen mode Exit fullscreen mode

I saw other important answers like those:

  • I don't like the scan content because it is hard to understand but it's a short answer.
  • I didn't know the tr method, it replace the first param with the secound param 🤘
class Acronym
  def self.abbreviate(string)
    string.scan(/(\b\w)/).join.upcase
  end
end

class Acronym
  def self.abbreviate(name)
    name.tr('-', ' ')
        .split
        .map { |x| x.slice(0) }.join.upcase
  end
end
Enter fullscreen mode Exit fullscreen mode

This was fun!

💖 💪 🙅 🚩
juanvqz
Juan Vasquez

Posted on April 3, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Exercism Ruby - High Scores
exercism Exercism Ruby - High Scores

April 27, 2020

Exercism Ruby - Acronym
exercism Exercism Ruby - Acronym

April 3, 2020

Exercism Ruby - Resistor Color Duo
exercism Exercism Ruby - Resistor Color Duo

February 27, 2020

Exercism Ruby - Intro
exercism Exercism Ruby - Intro

February 21, 2020