【问题标题】:undefined method (NoMethodError) ruby未定义的方法(NoMethodError)红宝石
【发布时间】:2014-01-12 07:33:03
【问题描述】:

我不断收到以下错误消息:

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

但我似乎无法理解为什么我的方法是“未定义的”:

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

【问题讨论】:

  • 你在调用它之后定义方法选择
  • 对于高级语言,Ruby 应该能够允许前向声明。 Objective-C 允许这样做。

标签: ruby nomethoderror


【解决方案1】:

这是因为您在定义它之前调用了方法choices。编写如下代码:

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)

我使用break 退出while 循环。否则会造成无限循环。

【讨论】:

  • 嗯,我认为这不应该是原因,因为在 Ruby 中,在调用之前或之后定义变量都没有区别。
  • @ImranNaqvi 你确定吗?
  • @ArupRakshit 我对此很有信心,但我在几个月前读过那件事,所以我不记得在 ruby​​ 文档中提到它的位置。但它在那里
  • @ImranNaqvi 试试 irb puts x,然后告诉我。
【解决方案2】:
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

该方法只需要在执行之前被调用。这里我将定义包装在main方法中,但只在choices()定义后调用main。

【讨论】:

    【解决方案3】:

    我在 Eclipse 中运行 Ruby 进行 App Academy 练习时遇到了同样的错误。我忘了添加“对象”。到提供的测试用例。以下语法有效:

          #!/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("===============================================")
    

    【讨论】:

    • 迟到了,但如果您将方法前缀为self.prime,则无需Prime.new
    猜你喜欢
    • 2017-06-07
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    相关资源
    最近更新 更多