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$
You need to move the lines
to after your method definition so that the
speaks
method has been defined when you reach thespeaks(speak)
line.Then next problem you will probably encounter is
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 ofand substitute the places where you're currently using the local
counter
variable with references to@counter