2017-09-27 3 views
0

メソッドが定義されているときに、この未定義メソッドエラーが発生するのはなぜですか? (私は今、いくつかのテキストを無駄にしているので、あなたの投稿は主にコードです "という警告が消えてしまいます。実際の開発者はほとんどの時間をコードを読んで費やすので、これは簡単な問題です。ここでは)minitestメソッドがありませんが、メソッドが存在します。何が欠けていますか?

は私のエラーメッセージです:ここで

$ ruby roman_numerals_test.rb 
Run options: --seed 35835 

# Running: 

Run options: --seed 35835 

# Running: 

SSSSSSSSSSEE 

Error: 
RomanNumeralsTest#test_1: 
NoMethodError: undefined method `to_roman' for 1:Integer 
    roman_numerals_test.rb:8:in `test_1' 


bin/rails test roman_numerals_test.rb:7 

は私のテストファイルです:

module BookKeeping 
    VERSION = 2 
end 

class Numeral 

    def initialize(number) 
    @number = number 
    end 

    def to_roman 
    roman_hash = { 
     1000 => 'M', 
     900 => 'CM', 
     500 => 'D', 
     400 => 'CD', 
     100 => 'C', 
     90 => 'XC', 
     50 => 'L', 
     40 => 'XL', 
     10 => 'X', 
     9 => 'IX', 
     5 => 'V', 
     4 => 'IV', 
     1 => 'I' 
    } 
    my_string = "" 
    roman_hash.each do |key, value| 
     (@number/key).times {my_string << value; @number = @number - key} 
    end 
    my_string 

    end 
end 

gem 'minitest', '>= 5.0.0' 
require 'minitest/autorun' 
require_relative 'roman_numerals' 

# Common test data version: 1.0.0 070e8d5 
class RomanNumeralsTest < Minitest::Test 
    def test_1 
    assert_equal 'I', 1.to_roman 
    end 

    def test_2 
    assert_equal 'II', 2.to_roman 
    end 

    def test_3 
    skip 
    assert_equal 'III', 3.to_roman 
    end 

    def test_4 
    skip 
    assert_equal 'IV', 4.to_roman 
    end 

    def test_5 
    skip 
    assert_equal 'V', 5.to_roman 
    end 

    def test_6 
    skip 
    assert_equal 'VI', 6.to_roman 
    end 

    def test_9 
    skip 
    assert_equal 'IX', 9.to_roman 
    end 

    def test_27 
    skip 
    assert_equal 'XXVII', 27.to_roman 
    end 

    def test_48 
    skip 
    assert_equal 'XLVIII', 48.to_roman 
    end 

    def test_59 
    skip 
    assert_equal 'LIX', 59.to_roman 
    end 

    def test_93 
    skip 
    assert_equal 'XCIII', 93.to_roman 
    end 

    def test_141 
    skip 
    assert_equal 'CXLI', 141.to_roman 
    end 

    def test_163 
    skip 
    assert_equal 'CLXIII', 163.to_roman 
    end 

    def test_402 
    skip 
    assert_equal 'CDII', 402.to_roman 
    end 

    def test_575 
    skip 
    assert_equal 'DLXXV', 575.to_roman 
    end 

    def test_911 
    skip 
    assert_equal 'CMXI', 911.to_roman 
    end 

    def test_1024 
    skip 
    assert_equal 'MXXIV', 1024.to_roman 
    end 

    def test_3000 
    skip 
    assert_equal 'MMM', 3000.to_roman 
    end 

    def test_bookkeeping 
    assert_equal 2, BookKeeping::VERSION 
    end 

end 

そしてここでは私のコードです

+0

class Integer 

または

class Numeric 

を試してみてください。 –

答えて

2

Numeralオブジェクトにto_romanを定義していますが、1はIntegerです。何 `Numeral`コアクラスはRubyでありません(Integerのスーパークラス)

+0

ええ、Numeralはオプションではありません(クラスメソッドを定義しない限り)。整数または数値です。 – Ursus

+0

hmm、今取得しました:NoMethodError:未定義メソッド '/ 'for Nil:NilClass – Jessi

+0

自分自身/キーに移動 – Ursus

関連する問題