我希望写一个继承类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
我希望写一个继承类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
类名是Bignum
不BigNum
。
如果我正确理解OP,他想创建自己的BigNum类,它继承了Bignum(这有点令人困惑)。 – 2012-01-08 09:55:46
由于红宝石,您可以扩展现有的类,你不必创建自己的类:
class Bignum
def divide?(a)
self %a == 0
end
end
这增加了一个方法鸿沟?到现有(内置)班级Bignum。
它应该按照你写的方式工作。编辑:只有班级的名字是错误的(感谢Dmitryi Budnik的回答) – 2012-01-08 09:54:55
小心'self%a'。在这种情况下,它不会伤害你,但是你应该总是在操作符的两边使用相同的空格,除非你打算他们是一元操作符(例如'a * b'变成'a(* b)'而不是'a * (b)') – 2012-01-08 10:01:06