从oustide访问模块内的模块的播放器名称

时间:2015-10-16 01:10:06

标签: ruby

我试图访问用户为他/她的用户名输入的内容,这是我模块之外的一个函数,因此可以在我的模块中使用

module RetryMessages
    def RetryMessages.message1()
        puts "That was an incorrect Choice #{@your_name}... Try again man."
    end
    MESSAGE2 = "Man you messed up #{@your_name} Try again."
    MESSAGE3 = "Hey man you screwed up..... Try again #{@your_name}."
end


def player_name
    puts "Hey man whats your name?"
    @your_name = $stdin.gets.chomp.upcase
    play_game
end

1 个答案:

答案 0 :(得分:0)

这是一个错误的设计。将您的消息转换为模板:

MESSAGES = [
  "That was an incorrect Choice %<your_name>s... Try again man.",
  "Man you messed up %<your_name>s Try again.",
  "Hey man you screwed up..... Try again %<your_name>s."
]

然后再

puts MESSAGES[0] % { your_name: "Joseph" }

这样,您使用该名称的地方实际上可以访问它。