🎉 I'm releasing 12 products in 12 months! If you love product, checkout my new blog workingoutloud.dev

Back to home

Ruby Anagrams

    This requires the gem installation of minitest.

    Test file

    Create file anagrams_test.rb:

    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__


    Create file anagrams.rb:

    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


    Change into directory and run ruby anagrams_test.rb.

    Personal image

    Dennis O'Keeffe

    • Melbourne, Australia

    Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
    I am currently working on Visibuild.


    Get fresh posts + news direct to your inbox.

    No spam. We only send you relevant content.

    Ruby Anagrams


    Share this post