Ruby 如何修复Sinatra在/未定义的局部变量或方法处返回NameError
我正在尝试将我创建的RPG字符表转换为网页,但遇到了问题 这是cardticks.rb文件:Ruby 如何修复Sinatra在/未定义的局部变量或方法处返回NameError,ruby,sinatra,game-development,Ruby,Sinatra,Game Development,我正在尝试将我创建的RPG字符表转换为网页,但遇到了问题 这是cardticks.rb文件: require 'sinatra' class Character def initialize(name, major_arcana, pentacles, wands, swords, cups, eyes, dress, hands) @name = name @major_arcana = major_arcana @pentacles =
require 'sinatra'
class Character
def initialize(name, major_arcana, pentacles, wands, swords, cups, eyes, dress, hands)
@name = name
@major_arcana = major_arcana
@pentacles = pentacles
@wands = wands
@swords = swords
@cups = cups
@eyes = eyes
@dress = dress
@hands = hands
end
end
get '/' do
erb :index, locals: {wizard_wizard: wizard_wizard}
end
这是.erb文件:
Begin!
<%
#these are the decks we pull from!
$deck_of_52 = ["Ace of Spades", "Two of Spades", "Three of Spades", "Four of Spades", "Five of Spades", "Six of Spades", "Seven of Spades", "Eight of Spades", "Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades", "King of Spades", "Ace of Hearts", "Two of Hearts", "Three of Hearts", "Four of Hearts", "Five of Hearts", "Six of Hearts", "Seven of Hearts", "Eight of Hearts", "Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts", "King of Hearts", "Ace of Clubs", "Two of Clubs", "Three of Clubs", "Four of Clubs", "Five of Clubs", "Six of Clubs", "Seven of Clubs", "Eight of Clubs", "Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs", "King of Clubs", "Ace of Diamonds", "Two of Diamonds", "Three of Diamonds", "Four of Diamonds", "Five of Diamonds", "Six of Diamonds", "Seven of Diamonds", "Eight of Diamonds", "Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds"]
def hand(x)
$deck_of_52.sample(x)
end
$major_arcana = ["The Fool", "The Magician", "The High Priestess", "The Empress", "The Emperor", " The Hierophant", "The Lovers", "The Chariot", "Fortitude", "The Hermit", "Wheel of Fortune", "Justice", "The Hanged Man", "Death", "Temperance", "The Devil", "The Tower", "The Star", "The Moon", "The Sun", "Judgement", "The World"]
def majorpull()
$major_arcana.sample(1)
end
#this begins character creation!
def mainmenu
puts "Do you want to make a move, display your current character, or generate a new character?"
choice = gets.chomp
if ["make a move", "move"].include?(choice)
puts $file_id.move
puts mainmenu
elsif ["display", "display my character"].include?(choice)
puts $file_id.display
puts mainmenu
elsif ["generate", "generate a new character"].include?(choice)
puts wizard_wizard
else
puts "Not sure what you meant. Try again!"
puts mainmenu
end
end
def hand(x)
$deck_of_52.sample(x)
end
def burn
if (2..4).include?(@wands)
puts hand(1)
elsif (5..8).include?(@wands)
puts hand(2)
elsif (9..11).include?(@wands)
puts hand(3)
end
end
def tools
if (2..4).include?(@pentacles)
puts hand(1)
elsif (5..8).include?(@pentacles)
puts hand(2)
elsif (9..11).include?(@pentacles)
puts hand(3)
end
end
def shock
if (2..4).include?(@cups)
puts hand(1)
elsif (5..8).include?(@cups)
puts hand(2)
elsif (9..11).include?(@cups)
puts hand(3)
end
end
def commune
if (2..4).include?(@swords)
puts hand(1)
elsif (5..8).include?(@swords)
puts hand(2)
elsif (9..11).include?(@swords)
puts hand(3)
end
end
def display
puts "My name is #{@name}, I bear the #{@major_arcana}. I have #{@eyes} eyes, #{@dress} clothes, and #{@hands} hands. My stats are #{@pentacles} of Pentacles, #{@wands} of Wands, #{@swords} of Swords, and #{@cups} of Cups."
end
def move
puts "What move do you want to take? Burn and Brand, Commune with the Beyond, Tools of the Trade, or Shock and Awe?"
input = gets.chomp.downcase
if ["burn", "burn and brand"].include?(input)
puts burn
elsif ["shock", "shock and awe"].include?(input)
puts shock
elsif ["commune", "commune with the beyond"].include?(input)
puts commune
elsif ["tools", "tools of the trade"].include?(input)
puts tools
else
puts "Sorry, that didn't make any sense to me. Care to try again?"
puts move
end
mainmenu
end
#this generates your character for Wizard World
def wizard_wizard
puts "What is your name?"
@playername = gets.chomp
puts "Give an adjective for how your eyes look."
@playereyes = gets.chomp
puts "Give an adjective for how your clothes look."
@playerdress = gets.chomp
puts "Give an adjective for how your hands look."
@playerhands = gets.chomp
puts "We'll now draw your cards for you."
@playermajor = majorpull
w = rand(1..11)
@playerwands = w
c = rand(1..11)
@playercups = c
s = rand(1..11)
@playerswords = s
p = rand(1..11)
@playerpentas = p
puts "Name the file we'll save your character to!"
$file_id = gets.chomp.to_i
$file_id = Character.new(@playername, @playermajor, @playerpentas, @playerwands, @playerswords, @playercups, @playereyes, @playerdress, @playerhands)
puts "Meet your new character!"
$file_id.display
mainmenu
end
puts wizard_wizard
%>
开始!
我从Sinatra那里得到的错误是:
undefined local variable or method `wizard_wizard' for #<Sinatra::Application:0x00007f6508de1470>
未定义的局部变量或方法“wizard\u wizard”#
我尝试给
向导\u向导范围(@
或$
),但这只会导致更多语法错误 向导\u向导
未定义
使用sinatra,首先启动一个ruby进程,它将加载一般内容,在您的情况下还将定义Character类。但是,执行流从您定义调用/
时应该发生的事情开始:调用向导向导
,用键:向导
将其交给本地
散列,然后调用erb
来解释索引模板。只有这样才能解析wizard\u wizard
函数
在单独的文件中定义函数,需要时需要该文件。此外,在sinatra网站模板中请求终端输入?那不行。这里您不需要sinatra—有了它,您必须适应web应用程序的工作方式(HTML、通过表单输入、Javascript),同时您似乎想要构建一个终端应用程序,它根本不需要框架。仅供参考,在视图文件中这样放置方法是非常不标准的。我将通过重构这一点来使用帮助器,也可以看到另一个次要的东西,您应该考虑使用关键字参数而不是位置代码,用于<代码>字符>初始化/<代码>方法。跟踪如此多的位置参数可能是一件很麻烦的事情!作为补充参考,语法在SO中很重要。可以将该站点视为一系列百科全书条目,其中包含解决该问题的答案。因此,清晰简洁的提问非常重要。全世界的人都会用这些问题来解决他们的问题,所以花时间写清楚对每个人都有帮助。