如何修复Sinatra在/未定义的本地变量或方法上返回NameError

时间:2019-11-14 07:20:42

标签: ruby sinatra game-development

我正在尝试将我创建的RPG字符表转换为网页,但是遇到了麻烦。

这是cardtricks.rb文件:

type Validity = [any, ...any[]];

interface StateValidator<V extends Validity, S> {
  (state: S): V;

  (state: S, dispatch: (validity: V)=>void): void;
}

function validateWithInitial<V extends Validity, S, I extends Validity>(
    state: S,
    validator: StateValidator<V, S>,
    initialValidity: I
): I | V { 
    return initialValidity;
}

function validate<V extends Validity, S>(
    state: S,
    validator: StateValidator<V, S>
): [null] | V { 
    const initialValidity: [null] = [null];
    return initialValidity;
}

这是.erb文件:

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

我从Sinatra得到的错误是:

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
%>

我尝试提供undefined local variable or method `wizard_wizard' for #<Sinatra::Application:0x00007f6508de1470> 范围(wizard_wizard@),但这只会导致更多语法错误。

1 个答案:

答案 0 :(得分:0)

wizard_wizard未定义。

使用sinatra,首先,一个红宝石处理开始,它将加载一般的东西,并且在您的情况下,还定义了Character类。但是随后的执行流程从您对调用/的定义开始:调用wizard_wizard,给键:wizard_wizard赋予locals哈希,然后调用erb来解释您的索引模板。只有这样wizard_wizard函数才会被解析。

在一个单独的文件中定义您的功能,在需要时需要该文件。另外,要求终端输入sinatra网站模板吗?那行不通。您不需要在这里使用sinatra-有了它,您似乎不得不构建根本不需要框架的终端应用程序,而必须适应Web应用程序的工作方式(HTML,通过表单输入,Javascript)。