2012-01-08 67 views
0

我希望寫一個繼承類Bignum的的代碼,我不知道怎麼去Bignum的價值Bignum的獲得價值紅寶石

class BigNum 
    # the metod should check if the a divide BigNum 
    def divide?(a) 
     # how to get the value of Bignum 
     self %a == 0 

    end 

    end 
+0

它應該按照你寫的方式工作。編輯:只有班級的名字是錯誤的(感謝Dmitryi Budnik的回答) – 2012-01-08 09:54:55

+0

小心'self%a'。在這種情況下,它不會傷害你,但是你應該總是在操作符的兩邊使用相同的空格,除非你打算他們是一元操作符(例如'a * b'變成'a(* b)'而不是'a * (b)') – 2012-01-08 10:01:06

回答

0

類名是BignumBigNum

+0

如果我正確理解OP,他想創建自己的BigNum類,它繼承了Bignum(這有點令人困惑)。 – 2012-01-08 09:55:46

2

由於紅寶石,您可以擴展現有的類,你不必創建自己的類:

class Bignum 
    def divide?(a) 
    self %a == 0 
    end 
end 

這增加了一個方法鴻溝?到現有(內置)班級Bignum。