8

I keep getting the following error message:

text.rb:2:in `<main>': undefined method `choices' for main:Object (NoMethodError)

But I can't seem to understand why my method is "undefined":

puts "Select [1] [2] [3] or [q] to quit"; users_choice = gets.chomp 
choices(users_choice)

def choices (choice)    
   while choice != 'q'      
        case choice

        when '1' 
            puts "you chose one!"

        when '2'
            puts "you chose two!"

        when '3'
            puts "you chose three!"
        end     
   end 
end
2
  • you're defining the method choices after calling it Jan 13, 2014 at 7:28
  • 4
    For an advanced language, Ruby should be able to allow forward declarations. Objective-C allows it.
    – Snowcrash
    May 13, 2015 at 18:52

3 Answers 3

20

This is because you are calling method choices, before defining it. Write the code as below:

puts "Select [1] [2] [3] or [q] to quit"
users_choice = gets.chomp 

def choices (choice)    
  while choice != 'q'      
    case choice
    when '1' 
      break  puts "you chose one!"
    when '2'   
      break puts "you chose two!"
    when '3'
      break  puts "you chose three!"
    end     
  end 
end

choices(users_choice)

I used break, to exit from the while loop. Otherwise it will create an infinite loop.

4
  • Hmm i think this should not be the reason , because in Ruby it makes no difference whether you define variables before calling it or after call .
    – Mani
    Oct 1, 2015 at 6:58
  • 1
    @ImranNaqvi Are you sure ? Oct 1, 2015 at 7:06
  • @ArupRakshit i'm quite confident for it , but i read that thing a couple of month ago , so i don't remember where it's mention in ruby documentation .but it is there
    – Mani
    Oct 1, 2015 at 7:17
  • 1
    @ImranNaqvi Try in irb puts x ,, and tell me. Oct 1, 2015 at 8:05
4
def main
puts "Select [1] [2] [3] or [q] to quit"; users_choice = gets.chomp
choices(users_choice)
end

def choices (choice)
  while choice != 'q'
    case choice

      when '1'
        puts "you chose one!"
        break
      when '2'
        puts "you chose two!"
        break
      when '3'
        puts "you chose three!"
        break
    end
  end
end

main

The method only needs to be called prior to being executed. Here I wrap the definition in the main method, but only call main after the definition of choices().

0

I was getting the same error running Ruby in Eclipse working out the App Academy practice exercises. I forgot to add "object." to the supplied test cases. The following syntax works:

      #!/usr/bin/ruby
       class Prime
      # Write a method that takes in an integer (greater than one) and
      # returns true if it is prime; otherwise return false.
      #
      # You may want to use the `%` modulo operation. `5 % 2` returns the
      # remainder when dividing 5 by 2; therefore, `5 % 2 == 1`. In the case
      # of `6 % 2`, since 2 evenly divides 6 with no remainder, `6 % 2 == 0`.
      # More generally, if `m` and `n` are integers, `m % n == 0` if and only
      # if `n` divides `m` evenly.
      #
      # You would not be expected to already know about modulo for the
      # challenge.
      #
      # Difficulty: medium.

      def primer(number)
        if number < 2 
          return false
        end
        i = 10
        while i > 1
          if number > i && number != i
            if number % i == 0 
              return false
            end 
          end
          i -= 1
        end
        return true
      end 
    end 

      object = Prime. new 

      # These are tests to check that your code is working. After writing
      # your solution, they should all print true.

      puts("\nTests for #primer")
      puts("===============================================")
          puts('primer(2) == true: ' + (object.primer(2) == true).to_s)
          puts('primer(3) == true: ' + (object.primer(3) == true).to_s)
          puts('primer(4) == false: ' + (object.primer(4) == false).to_s)
          puts('primer(9) == false: ' + (object.primer(9) == false).to_s)
      puts("===============================================")
1
  • late to the party but no need to Prime.new if you prefix your method as self.prime
    – Altons
    Jun 9, 2020 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.