ruby - 一个超类、2 个子类和一种常用方法 - 出现错误
ruby - One superclass , 2 subclasses and One common Method - Having an error
下面有Universe(superclass)、Common(module)、FlatEarthBelievers(subclass)和RoundEarthBelievers(subclass)。输出有错误,想要让它工作,有任何指示吗?
这是关于继承和混入的,子 class 继承自一个共同的父级并希望使用与父级的行为无关的通用方法的场景。我希望能够从 Child class that includes/extends it 中的公共模块调用方法。我对它的用法感到困惑,因为我想将那些常用方法视为 class 方法和实例方法。
class Universe
$knowledge = "Universe comprises of galaxies, stars, planets, steroids, meteoroids, comets, etc"
def universe_comprises_of
puts $knowledge
end
end
module Common
$earth_is = Hash.new
def init(earth_is)
$earth_is = earth_is
end
def statement
puts " Earth is " + $earth_is["attribute"].to_str
end
end
class FlatEarthBelievers < Universe
include Common
earth_is = { :attribute => "Flat !" }
init(earth_is)
end
class RoundEarthBelievers < Universe
include Common
earth_is = { :attribute => "Round like an Orange!" }
Common.init(earth_is)
end
moron = FlatEarthBelievers.new
moron.universe_comprises_of
moron.statement
sensible_person = RoundEarthBelievers.new
sensible_person.universe_comprises_of
sensible_person.statement
这段代码有很多错误。搁置 $
变量问题,因为在您的 OP 的评论中讨论了这个问题。
首先,您需要:
moron = Flat_earth_believers.new
但是您没有任何 class 名称为 Flat_earth_believers
。你的意思可能是 FlatEarthBelievers
。
您 include
Common
模块,它使其中的所有方法成为实例方法,但随后您尝试将它们称为 class 方法,这里:
class FlatEarthBelievers < Universe
include Common
earth_is = { :attribute => "Flat!" }
init(earth_is) # this is calling a class method
end
这里:
class RoundEarthBelievers < Universe
include Common
earth_is = { :attribute => "Round like an Orange!" }
Common.init(earth_is) # this is calling a class method
end
还有其他内容,但您明白了。
无论如何,我真的不知道你想做什么,但我想我会让 Universe
像:
class Universe
KNOWLEDGE = "Universe comprises of galaxies, stars, planets, steroids, meteoroids, comets, etc"
def universe_comprises_of
puts KNOWLEDGE
end
end
并且Common
喜欢:
module Common
def statement
puts "Earth is #{self.class::EARTH_IS[:attribute]}"
end
end
然后FlatEarthBelievers
喜欢:
class FlatEarthBelievers < Universe
include Common
EARTH_IS = { :attribute => "Flat!" }
end
那么你可以这样做:
moron = FlatEarthBelievers.new
moron.universe_comprises_of
=> Universe comprises of galaxies, stars, planets, steroids, meteoroids, comets, etc
moron.statement
=> Earth is Flat!
我也会注意到Jörg W Mittag想说:
I am one of those Ruby Purists who likes to point out that there is no such thing as a class method in Ruby. I am perfectly fine, though, with using the term class method colloquially, as long as it is fully understood by all parties that it is a colloquial usage. In other words, if you know that there is no such thing as a class method and that the term "class method" is just short for "instance method of the singleton class of an object that is an instance of Class
", then there is no problem. But otherwise, I have only seen it obstruct understanding.
让各方都充分理解,class方法这个词是上面用的口语。
下面有Universe(superclass)、Common(module)、FlatEarthBelievers(subclass)和RoundEarthBelievers(subclass)。输出有错误,想要让它工作,有任何指示吗?
这是关于继承和混入的,子 class 继承自一个共同的父级并希望使用与父级的行为无关的通用方法的场景。我希望能够从 Child class that includes/extends it 中的公共模块调用方法。我对它的用法感到困惑,因为我想将那些常用方法视为 class 方法和实例方法。
class Universe
$knowledge = "Universe comprises of galaxies, stars, planets, steroids, meteoroids, comets, etc"
def universe_comprises_of
puts $knowledge
end
end
module Common
$earth_is = Hash.new
def init(earth_is)
$earth_is = earth_is
end
def statement
puts " Earth is " + $earth_is["attribute"].to_str
end
end
class FlatEarthBelievers < Universe
include Common
earth_is = { :attribute => "Flat !" }
init(earth_is)
end
class RoundEarthBelievers < Universe
include Common
earth_is = { :attribute => "Round like an Orange!" }
Common.init(earth_is)
end
moron = FlatEarthBelievers.new
moron.universe_comprises_of
moron.statement
sensible_person = RoundEarthBelievers.new
sensible_person.universe_comprises_of
sensible_person.statement
这段代码有很多错误。搁置 $
变量问题,因为在您的 OP 的评论中讨论了这个问题。
首先,您需要:
moron = Flat_earth_believers.new
但是您没有任何 class 名称为 Flat_earth_believers
。你的意思可能是 FlatEarthBelievers
。
您 include
Common
模块,它使其中的所有方法成为实例方法,但随后您尝试将它们称为 class 方法,这里:
class FlatEarthBelievers < Universe
include Common
earth_is = { :attribute => "Flat!" }
init(earth_is) # this is calling a class method
end
这里:
class RoundEarthBelievers < Universe
include Common
earth_is = { :attribute => "Round like an Orange!" }
Common.init(earth_is) # this is calling a class method
end
还有其他内容,但您明白了。
无论如何,我真的不知道你想做什么,但我想我会让 Universe
像:
class Universe
KNOWLEDGE = "Universe comprises of galaxies, stars, planets, steroids, meteoroids, comets, etc"
def universe_comprises_of
puts KNOWLEDGE
end
end
并且Common
喜欢:
module Common
def statement
puts "Earth is #{self.class::EARTH_IS[:attribute]}"
end
end
然后FlatEarthBelievers
喜欢:
class FlatEarthBelievers < Universe
include Common
EARTH_IS = { :attribute => "Flat!" }
end
那么你可以这样做:
moron = FlatEarthBelievers.new
moron.universe_comprises_of
=> Universe comprises of galaxies, stars, planets, steroids, meteoroids, comets, etc
moron.statement
=> Earth is Flat!
我也会注意到Jörg W Mittag想说:
I am one of those Ruby Purists who likes to point out that there is no such thing as a class method in Ruby. I am perfectly fine, though, with using the term class method colloquially, as long as it is fully understood by all parties that it is a colloquial usage. In other words, if you know that there is no such thing as a class method and that the term "class method" is just short for "instance method of the singleton class of an object that is an instance of
Class
", then there is no problem. But otherwise, I have only seen it obstruct understanding.
让各方都充分理解,class方法这个词是上面用的口语。