2015-02-16 3 views
1

Я пытаюсь вызвать модуль в основной скрипт. Я связал файлы, используя require. Ниже то, что мой код выглядит следующим образом:Вызов модуля из другого файла

module Ex36_method 
    def m_defense 
     puts "Do you want to play zone or man to man for this possession?" 
     print "> " 
     choice = $stdin.gets.chomp 
     if choice.include? "zone" 
      zone 
     elsif choice.include? "man to man" 
      m_rebound 
     else 
      dead("You failed to play defense") 
     end 
    end 
    def zone 
     puts "The opposition scores a 3!" 
    end 
    def m_rebound 
     puts "The ball rims out and you got a rebound!" 
    end 
end 

require_relative 'Ex36_method' 

def start 
    puts "You are in the final minute of game 7 of the NBA finals." 
    puts "You are down by 3 points." 
    puts "What do you do: take a 3 pointer that might tie or take a guaranteed 2?" 
    print "> " 
    choice = $stdin.gets.chomp 
    if choice.include? "3 pointer" 
     puts "You missed! The ball rims out but you got the rebound at 40 seconds." 
     pass 
    elsif choice.include? "2 pointer" 
     puts "You scored! 50 seconds on the clock. Now it's time for defense" 
     m_rebound 
    else 
     dead("Turnover") 
    end 
end 
def dead(why) 
    puts why, "The opposing team scores a 3 and you lose. Better luck next year!" 
end 
start 

Когда я звоню функции в связанном модуле, я получаю следующее сообщение об ошибке:

"ex36.rb:16:in `start': undefined local variable or method `m_rebound' for main:Object (NameError) 
    from ex36.rb:27:in `<main>'" 

Любая помощь будет много оценили.

+1

Добро пожаловать в SO! Вы проделали отличную работу, изложив свой первый вопрос. – Anthony

+1

require_relative использует имя файла. Является ли ваш файл модуля «Ex36_method.rb»? – patrickmcgraw

+0

Команда k отформатирует ваш текст в качестве кода для дальнейшего использования. Можете ли вы показать, как вы называете методы? –

ответ

3

Вы загрузили модуль, но еще не включили методы модуля в текущую область. Добавить заявление о включении:

require_relative 'Ex36_method' 

include Ex36_method 

def start 
#... 
+0

Большое спасибо! Просто попробовал это, и это сработало как шарм. –

Смежные вопросы