我已经建立了一个程序来练习对象类和方法的创建。我是个菜鸟。这是我的密码:
class Orange_Tree
def initialize
@age = 0
@height = 0.3
@yield = 0
puts "The orange seed you planted has grown into a sapling, what will you call it?"
@name = gets.chomp
puts @name + "?!? What a glorious name! I see great things in this sprout's future."
puts @height
end
# Creates instance variables of this object upon creation.
# Prompts user for input and assigns answer to @name.
def talk
puts "You spend a little time each day talking to " + @name +". "
@height += @height * 1.5
if @yield == 0
if ((@height >= 1) and (@height < 3))
@yield += 2
elsif ((@height >= 3) and (@height < 7))
@yield += 5
elsif @height >= 7
@yield += 10
else
end
puts "Well that certainly helped! Even if your neighbours did think you were crazy. Here we are 1 year later and " + @name.to_s + " is now " + @height.to_s + " metres tall and now has " + @yield.to_s + " oranges."
else
puts @name + "'s produced all their fruit for this year."
end
end
#Method for helping the tree grow.
# Multiplies height by a factor of 1.5.
# Increases yield by continually greater number relative to the @height instance variable.
#Updates user with a string including new values of instance variables.
def sing
puts "You serenade " + @name + " once a day."
@height += @height * 2
if @yield == 0
if ((@height >= 1) and (@height < 3))
@yield += 4
elsif ((@height >= 3) and (@height < 7))
@yield += 10
elsif @height >= 7
@yield += 20
else
end
puts "That's the trick. I've never seen such growth! Here we are 1 year later and " + @name.to_s + " is now " + @height.to_s + " metres tall and now has " + @yield.to_s + " oranges."
else
puts @name + "'s produced all their fruit for this year."
end
end
# Similar to talk method above.
def eat oranges
if @yield >= oranges.to_i
puts "You can't take it any longer. Those oranges look too delicious"
@yield -= oranges
puts "Mmmm....tasty."
else
puts "There aren't enough oranges."
end
end
# Method which allows user to eat their chosen number of oranges, if there are enough.
def count
puts "There are " + @yield.to_s + " oranges."
end
# Allows user to check the number of oranges.
def too_old
if @height > 150
puts "There comes a time in person's life when you have to let go of childish fancies such as orange trees. That time has sadly come. Many good times were had and oranges ate. Fairwell dear " + @name + ". We'll never forget you."
exit
else
end
end
# This method is called by the pass_time method.
# If the if statement evaluates to true the user is shown message and the program ends.
def pass_time
@age += 1
@yield = 0
Orange_Tree.too_old
end
# Method which increases @age 1 year per call and ends the program when @age increases past 10.
# Each year that passes resets @yield to 0.
end
tree = Orange_Tree.new
tree.talk
tree.count
tree.eat 1
tree.pass_time
tree.sing
tree.eat 1
tree.count
tree.pass_time
tree.talk
tree.count
tree.eat 1
tree.pass_time
tree.sing
tree.eat 1
tree.count
tree.pass_time
tree.talk
tree.count
tree.eat 1
tree.pass_time
tree.sing
tree.eat 1
tree.count
tree.pass_time
tree.sing当ran给出:
orange_tree.rb:97:in `pass_time': undefined method `too_old' for Orange_Tree:Class (NoMethodError)
from orange_tree.rb:109:in `<main>'在这里搜索了一下之后,我想我知道问题出在哪里了。我已经将"too_old“方法定义为实例方法,它需要是我的程序按需要工作的类方法(每当调用"pass_time”方法时,它都会调用“太老”方法)。
因此,我将"too_old“方法更改为:
def self.too_old
if @height > 150
puts "There comes a time in person's life when you have to let go of childish fancies such as orange trees. That time has sadly come. Many good times were had and oranges ate. Fairwell dear " + @name + ". We'll never forget you."
exit
else
end
end这给了我:
orange_tree.rb:82:in `too_old': undefined method `>' for nil:NilClass (NoMethodError)
from orange_tree.rb:97:in `pass_time'
from orange_tree.rb:109:in `<main>'所以现在我被困住了。我很感谢你的帮助,谢谢。
发布于 2015-05-23 11:53:36
too_old是类Orange_tree上的实例方法。这意味着您必须在Orange_tree实例上调用它。在原始代码中,您在整个类Orange_tree上调用它
试试这个:
def pass_time
@age += 1
@yield = 0
self.too_old
end对您的pass_time方法的一次修改使您的程序能够毫无错误地运行。
发布于 2015-05-23 11:45:47
问题是,当您调用Orange_Tree.too_old方法时,您从未创建过Orange_Tree类的真正实例。当不创建实例时,就不会调用类初始化方法(当调用Orange_Tree.new时将调用初始化)。在您的示例中,初始化方法会将@ case变量初始化为0.3,但从来没有发生过,所以可以得到undefined method>‘for :NilClass(NoMethodError)’
我的建议是用普通的def self.too_old方法代替def too_old方法,并在pass_time方法中创建Orange_Tree的新实例
总括而言:
def pass_time
@age += 1
@yield = 0
Orange_Tree.new.too_old
end在红宝石中,我们将类命名为OrangeTree而不是Orange_Tree (约定)
https://stackoverflow.com/questions/30412176
复制相似问题