【问题标题】:Ruby TTY prompt undefined variable or methodRuby TTY 提示未定义的变量或方法
【发布时间】:2021-06-02 12:46:03
【问题描述】:

所以对于一个任务,我正在构建一个简单的选择你自己的冒险终端游戏。

我有一个无法解决的未定义局部变量或方法错误。

prompt.select("Do you stop to listen? Do you leave the path to investigate? or do you spur your horse on faster?") do |menu|
    menu.choice "Stop to listen", -> {paragraph_4}
    menu.choice "Leave path and investigate", -> {paragraph_5}
    menu.choice "Spur horse on faster", -> {paragraph_7}
  
  end 
4 个 ruby​​ 文件中的每一个都存储了三个段落-

prompt = TTY::Prompt.new

$paragraph_4 = def paragraph_4

prompt = TTY::Prompt.new

puts ""
puts "" 
puts "   Stopping to listen it seems whatever is moving in the brush is doing the same. Your horse stamps a foot impatiently."

prompt.select("Will you dismount? Do you call out? or do you continue on your way?") do |menu|
  menu.choice "Dismount", -> {paragraph_7}
  menu.choice "Call out", -> {paragraph_8}
  menu.choice "Continue", -> {paragraph_7}

end

$paragraph_5 = def paragraph_5
uts ""
puts "" 
puts ""
puts "  Suddenly a giant spider leaps from the brush and burys its huge fangs in your neck."
puts "" 
puts "  YOU HAVE DIED

这仅适用于每个文件的第一段。就像我在提示符下进行第一个选择一样,但是第二个或第三个-

game.rb:77:in block (2 levels) in <main>': undefined local variable or method paragraph_2' for main:Object (NameError)

我不明白为什么?

prompt = TTY::Prompt.new

$paragraph_1 = def paragraph_1 

prompt = TTY::Prompt.new

   puts ""
   puts ""
   puts "  'Ah the north road, a wise choice friend as your way will meander towards Ered Mithrin and Durins folk of the Grey Mountains'
  'Perhaps like Gandalf and his companions who whence this way you will find Erebor the lonely mountain, whereby Smaug the Terrible met his end.'
  'Now friend the hour grows late and you best away lest you not find a clearing within the woods to light a fire for camp at night.
  'Bidding the kind old man well and flicking a silver coin his way you nudge your horse with your knees and head towards the woods.'
  'As you approach the branchs of the dark and twisted trees seem to reach out and their trunks rear up and lean forward to engulf you. Soon the space'
  'between the boughs becomes thinner and thinner and although what seemed like moments ago late afternoon now seems like a twighlight under the weight of the ancient forest."

  "As you walk deeper into the dark woods you hear a rustling and from the corner of your eye you see movement to your left"

  prompt.select("Do you stop to listen? Do you leave the path to investigate? or do you spur your horse on faster?") do |menu|
    menu.choice "Stop to listen", -> {paragraph_4}
    menu.choice "Leave path and investigate", -> {paragraph_5}
    menu.choice "Spur horse on faster", -> {paragraph_7}

  end 


  
  $paragraph_2 = def paragraph_2 

  prompt = TTY::Prompt.new

  puts ""
  puts ""
  puts "  'Ah the middle way, a sensible choice friend as your way will take you through the lands of the Sindarin or Silvan Elves and Thranduil's realm'
  'Now friend the hour grows late and you best away lest you not find a clearing within the woods to light a fire for camp at night.
  'Bidding the kind old man well and flicking a silver coin his way you nudge your horse with your knees and head towards the woods.'
  'As you approach the branchs of the dark and twisted trees seem to reach out and their trunks rear up and lean forward to engulf you. Soon the space'
  'between the boughs becomes thinner and thinner and although what seemed like moments ago late afternoon now seems like a twighlight under the weight of the ancient forest."
  ""
  "As you walk deeper into the dark woods you hear a rustling and from the corner of your eye you see movement to your left"

  prompt.select("Do you stop to listen? Do you leave the path to investigate? or do you spur your horse on faster?") do |menu|
    menu.choice "Stop to listen", -> {paragraph_4}
    menu.choice "Leave path and investigate", -> {paragraph_5}
    menu.choice "Spur horse on faster", -> {paragraph_7}
  
  end 

【问题讨论】:

  • Lambda 是闭包。 #paragraph_2 方法在哪里定义?
  • 我已经添加了另一个代码 sn-p 如果你能看一下 Todd 将不胜感激。
  • 你在 lambdas 中调用方法。方法不在范围内。
  • 您也将方法分配给全局变量,但它们不是 lambda,您永远不会尝试调用它们。目前尚不清楚您为什么认为这会奏效,而且您的示例并不能真正充分解释您的思维过程以提供进一步帮助。
  • 我是 Ruby 新手,学习编码才 3 个月,请原谅我的无知。我想要做的是通过 tty 提示从每个段落分支出来,然后再次分支,直到故事结束。如果您能建议这将如何工作,将不胜感激。

标签: ruby tty


【解决方案1】:

这是您想要做的一个示例:

require 'tty-prompt'

class MyGame
  attr_accessor :prompt

  def initialize
    @prompt = TTY::Prompt.new
  end

  def paragraph_1
    prompt.select("What do you do?") do |menu|
      menu.choice "Stop to listen",->{paragraph_2}
      menu.choice "Leave path and investigate",->{paragraph_3}
      menu.choice "Spur horse on faster",->{paragraph_4}
    end
  end

  def paragraph_2
    # TODO: implement choices; use paragraph_1 as an example
    prompt.select('Paragraph 2') do |menu|
      menu.choice 'P2 Choice 1'
      menu.choice 'P2 Choice 2'
      menu.choice 'P2 Choice 3'
    end
  end

  def paragraph_3
    # TODO: implement choices; use paragraph_1 as an example
    prompt.select('Paragraph 3') do |menu|
      menu.choice 'P3 Choice 1'
      menu.choice 'P3 Choice 2'
      menu.choice 'P3 Choice 3'
    end
  end

  def paragraph_4
    # TODO: implement choices; use paragraph_1 as an example
    prompt.select('Paragraph 4') do |menu|
      menu.choice 'P4 Choice 1'
      menu.choice 'P4 Choice 2'
      menu.choice 'P4 Choice 3'
    end
  end
end

my_game = MyGame.new
my_game.paragraph_1

我不建议在$ 全局变量中定义你的方法。类/模块是组织代码的更好方法。

另外,我会建议一个更好的命名方案。例如:

  • cool_path1
  • cool_path1_choice1
  • cool_path1_choice2
  • cool_path1_choice3

或类似的东西。如果需要,您可以将其分解为更多的类/模块到不同的文件中。

不过,我也可以看到这变得相当复杂。在内部,您可以存储某种类型的状态(我们的英雄做出了哪些选择)并从那里做出决定。

不要害怕向老师寻求帮助。这是老师的目的之一。也许你的老师可以帮助你编写代码和/或设计。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多