问题
Was completing the pine exercise for Def Grandma and thought would try and take it a little further and abstract it to remove as much duplication as possible.
Hope my logic with this isn't too bizzare just tried to separate things into functions. But now if I type
Bye
The program exits immediately without proceeding to the exitPlan function. Any advice welcome.
puts 'Say something nice to Grandma.'
puts 'You may need to shout > '
speak = gets.strip
counter = 0
speaks(speak)
def speaks(speak)
if speak != 'Bye'
talk()
else
exitPlan()
end
end
def talk()
if speak == speak.downcase
puts 'Huh Speak up Sonny'
counter -= 1
else
year = rand(1930..1951)
puts 'No not Since ' + year.to_s
counter -= 1
end
if counter < 0
counter = 0 # don't want counter going below zero.
end
puts 'Say something nice to Grandma'
speaks()
end
def exitPlan()
counter += 1
unless counter == 3
puts 'Say something nice to Grandma'
speaks()
else
puts 'good night Sonny'
end
end
Error
renshaw@renshaw-TravelMate-5740G:~/Ruby$ ruby -W dGrand.rb
Say something nice to Grandma.
You may need to shout >
Bye
dGrand.rb:6:in `<main>': undefined method `speaks' for main:Object (NoMethodError)
renshaw@renshaw-TravelMate-5740G:~/Ruby$
回答1:
You need to move the lines
puts 'Say something nice to Grandma.'
puts 'You may need to shout > '
speak = gets.strip
counter = 0
speaks(speak)
to after your method definition so that the speaks
method has been defined when you reach the speaks(speak)
line.
Then next problem you will probably encounter is
in `exitPlan': undefined method `+' for nil:NilClass
This is because counter
is a local variable so is not shared between your toplevel code and the different methods. You will need to either use a global variable i.e. $counter
for this or, better, put the various methods inside a class and then use an instance variable.
I suspect there are still some other issues in your code such as you only seem to be calling gets.strip
to get the input once. However, in terms of wrapping the code inside a class it's not the counter that you want to wrap as you would still need to pass that around between the various methods. It's the whole speak/talk interaction so something along the lines of
class Grandma
def initialize
@counter = 0
end
def speaks(speak)
..
end
def talk()
..
end
def exitPlan()
..
end
end
grandma = Grandma.new
grandma.speaks(speak)
and substitute the places where you're currently using the local counter
variable with references to @counter
回答2:
You are calling method speaks
before it's defined. See Ruby: How to call function before it is defined?
回答3:
Look at the other posts explaining the use of methods and globals. Here you have a gist that works with Ruby 1.9.3 because of how you used rand
来源:https://stackoverflow.com/questions/9095047/errors-in-deaf-grandma-pine-no-method