2015-02-09 152 views
0

我這4類,代碼很簡單,但是當我嘗試生成,它返回類似紅寶石 - 未初始化的常量

in `<main>': uninitialized constant PruebasUnitarias (NameError) 

的錯誤代碼是這樣的,我不知道什麼是錯的,我想這是因爲需要,但我全部在一個文件中,仍然崩潰。

class Complejo 
    def initialize (real, imaginario) 
    @real = real 
    @imaginario = imaginario 
    end 

    def sumar (complejo) 
    @real = @real + complejo.real 
    @imaginario = @imaginario + complejo.imaginario 
    end 
    attr_reader :real, :imaginario 
end 

class Prueba 
    def assertCierto(valor) 
    return valor 
    end 

    def assertFalso(valor) 
    return valor 
    end 

    def assertIgual(num1, num2) 
    if(num1 == num2) 
     return true 
    end 
    return false 
    end 

    def assertDistinto(num1, num2) 
    if(num1 != num2) 
     return true 
    end 
    false 
    end 

    def assertNil param 
    if (param == nil) 
     return true 
    end 
    false 
    end 

    def assertContiene(param1, param2) 
    param1.include?(param2) 
    end 

end 

class PruebasUnitarias::Prueba 
    def run 
    metodos = self.methods 
    if(self.respond_to? :inicializacion) 
     self.inicializacion 
    end 

    end 
end 

class PruebaComplejo < PruebasUnitarias::Prueba 
    def inicializacion 
    @c1 = Complejo.new(3,5) 
    @c2 = Complejo.new(1,-1) 
    end 

    def prueba_suma 
    @c1.sumar(@c2) 
    assertIgual(@c1.real, 4) 
    assertIgual(@c1.imaginario, 4) 
    end 

    def prueba_suma_cero 
    @c2.sumar(Complejo.new(0,0)) 
    assertCierto(@c2.real==1) 
    assertCierto(@c2.imaginario==-1) 
    end 

    def prueba_suma_nula 
    @c2.sumar(nil) 
    assertIgual(@c2.real, 1) 
    assertIgual(@c2.imaginario, -1) 
    end 

    def imprimir (complejo) 
    puts "complejo: #{complejo.real}, #{complejo.imaginario}i" 
    end 
end 
+0

該錯誤是在這條線 類PruebasUnitarias :: Prueba – 2015-02-09 22:01:09

回答

0

您必須先聲明模塊,然後才能放入類。試試這個:

module PruebasUnitarias 
    class Prueba 
    ... 
    end 
end 
class PruebaComplejo < PruebasUnitarias::Prueba 
    ... 
end 
+0

謝謝你,但我不能改變德班聲明,以PruebaComplejo必須是一流的PruebaComplejo 2015-02-09 22:14:21

+0

@VictorRamirezLopez:固定。請參閱編輯。 – Linuxios 2015-02-09 22:15:11

+0

oo謝謝你,finaly它的作品!非常感謝你 – 2015-02-09 22:18:52

0

正如你的編程風格的說明,你需要考慮在簡單來說,當涉及到測試你的價值觀。考慮到這些變化:

def assertCierto(valor) 
    valor 
end 

def assertFalso(valor) 
    valor 
end 

def assertIgual(num1, num2) 
    num1 == num2 
end 

def assertDistinto(num1, num2) 
    num1 != num2 
end 

def assertNil param 
    param == nil 
end 

這是沒有必要使用一個if測試,看看如果事情是相等或不相等,然後返回truefalse。測試本身就是這樣簡單地比較值,並讓Ruby默認返回結果。

1 == 1 # => true 
1 == 2 # => false 
1 != 1 # => false 
1 != 2 # => true 

此外,使用snake_case方法名稱,而不是camelCase。 ItsAReadabilityThing:

def assert_cierto(valor) 

    def assert_falso(valor) 

    def assert_igual(num1, num2) 

    def assert_distinto(num1, num2) 

    def assert_nil param 
+0

謝謝你,已經改變了它。它是我在Ruby中的第二個程序,我總是用Java編程 – 2015-02-09 22:15:05

相關問題