Ruby noob gets NoMethodError

170 views Asked by At

I am doing a Ruby exercise on exercism- my first since doing codecademy. and the set up is that i have two files in the same folder- hamming_test.rb that requires_relative hamming.rb-- i got that far, but no farther. i have set up an empty class with an empty method in hamming.rb, but when hamming_test.rb runs and gets to the first method call, it returns a NoMethodError for that line. I suspect that the code is okay (as far as it goes), but the problem is about something in my computer setup. I have tried running it from TextEdit and the console and from within NetBeans: same error. I have tried all sorts of things, but always the same error- it's like the interpreter can find the file, but can't see inside it. I have a Mac running Yosemite, and Ruby seems to run okay otherwise. I can't even seem to get out of the gate with this, please help. Code below, in case that helps:

hamming.rb:

class Hamming
  def compute
  end
end

hamming_test.rb:

require 'minitest/autorun'
begin
  require_relative 'hamming'
rescue LoadError => e
  puts "\n\n#{e.backtrace.first} #{e.message}"
  puts DATA.read
  exit 1
end

class HammingTest < MiniTest::Unit::TestCase
  def test_no_difference_between_identical_strands
    assert_equal 0, Hamming.compute('A', 'A')
  end

  def test_complete_hamming_distance_of_for_single_nucleotide_strand
    skip
    assert_equal 1, Hamming.compute('A','G')
  end

  def test_complete_hamming_distance_of_for_small_strand
    skip
    assert_equal 2, Hamming.compute('AG','CT')
  end

  def test_small_hamming_distance
    skip
    assert_equal 1, Hamming.compute('AT','CT')
  end

  def test_small_hamming_distance_in_longer_strand
    skip
    assert_equal 1, Hamming.compute('GGACG', 'GGTCG')
  end

  def test_ignores_extra_length_on_first_strand_when_longer
    skip
    assert_equal 1, Hamming.compute('AGAGACTTA', 'AAA')
  end

  def test_ignores_extra_length_on_other_strand_when_longer
    skip
    assert_equal 2, Hamming.compute('AGG', 'AAAACTGACCCACCCCAGG')
  end

  def test_large_hamming_distance
    skip
    assert_equal 4, Hamming.compute('GATACA', 'GCATAA')
  end

  def test_hamming_distance_in_very_long_strand
    skip
    assert_equal 9, Hamming.compute('GGACGGATTCTG', 'AGGACGGATTCT')
  end
end

END


You got an error, which is exactly as it should be. This is the first step in the Test-Driven Development (TDD) process.

The most important part of the error is

  cannot load such file

It's looking for a file named bob.rb that doesn't exist yet.

To fix the error, create an empty file named bob.rb in the same directory as the bob_test.rb file.

Then run the test again.

For more guidance as you work on this exercise, see GETTING_STARTED.md.


1

There are 1 answers

0
Amadan On BEST ANSWER

You are calling a class method Hamming.compute('A', 'A'). Such a method is not defined.

  1. Your defined compute method is an instance method (you'd need to do Hamming.new.compute).
  2. It is defined as a zero-parameter function.

This definition should fix both issues:

class Hamming
  def self.compute(x, y)
  end
end