Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7367841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:35:18+00:00 2026-05-29T03:35:18+00:00

Was completing the pine exercise for Def Grandma and thought would try and take

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T03:35:19+00:00Added an answer on May 29, 2026 at 3:35 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would I go about completing the following function in vimscript? fun! Foo() let
I've run into some trouble in my programming book while completing an exercise to
I am currently completing an application that was started by someone else. He is
I want users to receive 'points' for completing various tasks in my application -
Do the international students get a job offer in Australia after completing their Masters degree from University of Melbourne?
I have recently started working on a very large C++ project that, after completing
What would be some compelling reasons to upgrade a web app from .NET 1.1
I'm completing the development of a site I didn't build (I designed it, but
I'm considering two different ways of completing the same task of displaying dynamic HTML
I'm looking for a more efficient way of completing this task. I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.