Jac7k
Jac7k

Reputation: 37

A Ruby class to compare the characters in two strings

I'm trying to complete an exercism.io test file which compares two strings and adds one to a counter each time there is a difference between the two strings. I've written my class, but for some reason it won't run in terminal. I've compared my code with several examples of syntax online and don't see why it won't run. Any help would be much appreciated.

Here's my class:

class Hamming
    def compute(str1, str2)
        distance = 0
        length = str1.length
        for i in 0..length
            if str1[i] != str2[i] then
                distance++
            end
        end
    return distance
    end
end

And here's a relevant bit of test file:

class HammingTest < Minitest::Test
  def test_identical_strands
    assert_equal 0, Hamming.compute('A', 'A')
  end
end

Lastly, here's the error I'm getting:

hamming_test.rb:4:in `require_relative': /Users/Jack/exercism/ruby/hamming/hamming.rb:8: syntax error, unexpected keyword_end (SyntaxError)
/Users/Jack/exercism/ruby/hamming/hamming.rb:12: syntax error, unexpected end-of-input, expecting keyword_end
    from hamming_test.rb:4:in `<main>'

Upvotes: 0

Views: 1902

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51151

  1. You don't need then after condition in if statement.
  2. Use two spaces instead of four for indentation in Ruby.
  3. (Direct cause of your error) there's no ++ operator in Ruby. You should have

    distance += 1
    

Upvotes: 1

Related Questions