Back to home

Ruby Anagrams

Published: Aug 7, 2018

Last updated: Aug 7, 2018

    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__

    Anagrams

    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

    Testing

    Change into directory and run ruby anagrams_test.rb.

    Personal image

    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 Visibuild.

    1,200+ PEOPLE ALREADY JOINED ❤️️

    Get fresh posts + news direct to your inbox.

    No spam. We only send you relevant content.

    Ruby Anagrams

    Introduction

    Share this post