私は簡単にこのように、たとえば、から例えばString
を継承することができますRational(またはコンストラクタを持たない任意のクラス)からどのように継承できますか?
class MyString < String
def stuff
self + ' and stuff'
end
end
# This works:
MyString.new('things').stuff # => 'things and stuff'
しかし、どのように私は、コンストラクタを持っていないRational
、継承することができますか?例:
def MyRat < Rational
def inc
self + 1
end
end
# I have tried to initialize like this:
MyRat.new(10).inC# => NoMethodError: undefined method `new' for MyRat:Class
MyRat(10).inC# => NoMethodError: undefined method `MyRat' for main:Object
MyRat.send(:initialize, 10).inc # => TypeError: already initialized class
# ???
# None of it works!
新しいクラスを初期化する方法が見つかりません。
あなた自身の '#initialize'メソッドを書かないのはなぜですか? –
@ArupRakshitできません。どうすればいい? – Doorknob