这太疯狂了,我觉得问这个真的很愚蠢。但我有一个基本的 Ruby 代码,它由
<醇>if text includes specific words then you apply math if text includes specific words you don't apply math
for each do |x|
)和基本 variable = true
,或 variable = input * 52/300
我从哪里开始使用 OOP 将其变成基于类的结构?
最佳答案
我会给你一个基本的大纲来让你开始,但我不会为你做你的工作。 :)
首先:定义一个类来保存您的输入。
class MyClass
def initialize(data)
@data = data
...put your initialization code here, this gets run when you call MyClass.new()
end
attr_accessor :data
...
end
第二:定义一个“容器”类来容纳类的多个实例。
class MyContainer
def initialize(record)
@records << record
end
...
end
第三步:对于收到的每一行,创建一个类的新实例
mydata = MyClass.new(input)
第四:将新实例存储在您的容器类中。
MyContainer.new(mydata)
现在,您可以执行诸如在 MyContainer 中创建 add
方法之类的操作,该方法将遍历它在 @records 中保存的所有类并产生总和。
关于ruby - 向基本 Ruby 代码添加类结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20576392/