Back to home

Test fileAnagramsTesting
Ruby Anagrams main image

Ruby Anagrams

This requires the gem installation of minitest.

Test file

Create file anagrams_test.rb:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 begin gem 'minitest', '>= 5.0.0' require 'minitest/autorun' require_relative 'anagrams' rescue Gem::LoadError => e puts "\nMissing Dependency:\n#{e.backtrace.first} #{e.message}" puts 'Minitest 5.0 gem must be installed for the Ruby track.' rescue LoadError => e puts "\nError:\n#{e.backtrace.first} #{e.message}" puts DATA.read exit 1 end # Common test data version: 1.1.0 be3ae66 class AnagramsTest < Minitest::Test def test_simple_anagram a = "tokyo" b = "kyoto" # skip assert_equal true, Anagrams.compare(a,b) end def test_simple_capitals_anagram c = "tokyo" d = "Kyoto" # skip assert_equal true, Anagrams.compare(c,d) end def test_punctuation_anagram e = "t 3 55oky!o" f = "Ky@ ot%o" # skip assert_equal true, Anagrams.compare(e,f) end end __END__

Anagrams

Create file anagrams.rb:

1 2 3 4 5 class Anagrams def self.compare(a, b) return a.gsub(/[^a-z]/i, "").downcase.chars.sort.join == b.gsub(/[^a-z]/i, "").downcase.chars.sort.join end end

Testing

Change into directory and run ruby anagrams_test.rb.

Dennis O'Keeffe

@dennisokeeffe92
  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on workingoutloud.dev, Den Dribbles and LandPad .

Related articles


1,200+ PEOPLE ALREADY JOINED ❤️️

Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.