Ruby 实例变量是;“无”;而不是为给定类创建对象后的值

Ruby 实例变量是;“无”;而不是为给定类创建对象后的值,ruby,sinatra,Ruby,Sinatra,我有一个代码,它生成一个文件。我希望能够要求初始化变量 lib/main.rb require_relative "l5k_cms/version" require "./new_files" require "./methods" require "sinatra" require "erb" def load_pictures Dir.glob("public/image/*.{jpg,JPG}") end get "/" do @pictures = load_pic

我有一个代码,它生成一个文件。我希望能够要求初始化变量

lib/main.rb

require_relative "l5k_cms/version"
require "./new_files"
require "./methods"
require "sinatra"
require "erb"


def load_pictures
    Dir.glob("public/image/*.{jpg,JPG}")
end

get "/" do
    @pictures = load_pictures
    erb :index
end

get "/new" do
    erb :new
end

post "/new" do
    @nazwa = params[:name]
    @nazwisko = params[:family]
    @klasa = params[:class]
    @ogien = params[:fire]
    @powietrze = params[:air]
    @woda = params[:water]
    @ziemia = params[:earth]
    @pustka = params[:void]
    cechy = { :nazwa => @nazwa, :nazwisko => @nazwisko, :klasa => @klasa, :ogien => @ogien, :powietrze => @powietrze, :woda => @woda, :ziemia => @ziemia, :pustka => @pustka }
    dir = Dir_methods.new
    dir.create_NPC_folder()
    Dir.chdir("public/NPC") do 
        npc = File.new("#{@nazwa}", "w+")
        npc_rb = File.new("#{@nazwa}.rb", "w+")
        writer = File_Methods.new()
        writer.txt_file_filer(npc, cechy)
        npc.close()
        writer.rb_file_filer(npc_rb, cechy)
        npc_rb.close()
    end
    erb :show_new_NPC
end

get '/show' do
    @test = []
    @npc = ["plik"]
    Dir.chdir("public/NPC") do
        @npc = Dir.glob("*.rb")
        @npc.each do |element| 
            require("./#{element}")
            class_name = Object.const_get(element.capitalize[/^[^.]*/])
            a = class_name.new()
            @test.push("To jest imie: #{a.nazwa}, a to nazwisko: #{a.nazwisko}, a to ogien #{a.ogien}")
        end
    end
    erb :show_NPC
end
  class Foo

    attr_accessor :nazwa, :nazwisko

    def initialize

    @nazwa = Foo
    @nazwisko = moreFoo

    end

end
<html>
    <head>
        <tile>Show</title>
        <meta charset="UTF-8">
        <link type="text/css" rel="stylesheet" href="CSS/styles.css"/>
    </head>
    <body>
        <h1>Hello </h1>
        <h3> This is the test array >> <%= @test %> </h3>
        <a href="/">Strona Glowna </a>
    </body>
</html>
lib/Public/NPC/foo.rb

require_relative "l5k_cms/version"
require "./new_files"
require "./methods"
require "sinatra"
require "erb"


def load_pictures
    Dir.glob("public/image/*.{jpg,JPG}")
end

get "/" do
    @pictures = load_pictures
    erb :index
end

get "/new" do
    erb :new
end

post "/new" do
    @nazwa = params[:name]
    @nazwisko = params[:family]
    @klasa = params[:class]
    @ogien = params[:fire]
    @powietrze = params[:air]
    @woda = params[:water]
    @ziemia = params[:earth]
    @pustka = params[:void]
    cechy = { :nazwa => @nazwa, :nazwisko => @nazwisko, :klasa => @klasa, :ogien => @ogien, :powietrze => @powietrze, :woda => @woda, :ziemia => @ziemia, :pustka => @pustka }
    dir = Dir_methods.new
    dir.create_NPC_folder()
    Dir.chdir("public/NPC") do 
        npc = File.new("#{@nazwa}", "w+")
        npc_rb = File.new("#{@nazwa}.rb", "w+")
        writer = File_Methods.new()
        writer.txt_file_filer(npc, cechy)
        npc.close()
        writer.rb_file_filer(npc_rb, cechy)
        npc_rb.close()
    end
    erb :show_new_NPC
end

get '/show' do
    @test = []
    @npc = ["plik"]
    Dir.chdir("public/NPC") do
        @npc = Dir.glob("*.rb")
        @npc.each do |element| 
            require("./#{element}")
            class_name = Object.const_get(element.capitalize[/^[^.]*/])
            a = class_name.new()
            @test.push("To jest imie: #{a.nazwa}, a to nazwisko: #{a.nazwisko}, a to ogien #{a.ogien}")
        end
    end
    erb :show_NPC
end
  class Foo

    attr_accessor :nazwa, :nazwisko

    def initialize

    @nazwa = Foo
    @nazwisko = moreFoo

    end

end
<html>
    <head>
        <tile>Show</title>
        <meta charset="UTF-8">
        <link type="text/css" rel="stylesheet" href="CSS/styles.css"/>
    </head>
    <body>
        <h1>Hello </h1>
        <h3> This is the test array >> <%= @test %> </h3>
        <a href="/">Strona Glowna </a>
    </body>
</html>
lib/views/show.erb

require_relative "l5k_cms/version"
require "./new_files"
require "./methods"
require "sinatra"
require "erb"


def load_pictures
    Dir.glob("public/image/*.{jpg,JPG}")
end

get "/" do
    @pictures = load_pictures
    erb :index
end

get "/new" do
    erb :new
end

post "/new" do
    @nazwa = params[:name]
    @nazwisko = params[:family]
    @klasa = params[:class]
    @ogien = params[:fire]
    @powietrze = params[:air]
    @woda = params[:water]
    @ziemia = params[:earth]
    @pustka = params[:void]
    cechy = { :nazwa => @nazwa, :nazwisko => @nazwisko, :klasa => @klasa, :ogien => @ogien, :powietrze => @powietrze, :woda => @woda, :ziemia => @ziemia, :pustka => @pustka }
    dir = Dir_methods.new
    dir.create_NPC_folder()
    Dir.chdir("public/NPC") do 
        npc = File.new("#{@nazwa}", "w+")
        npc_rb = File.new("#{@nazwa}.rb", "w+")
        writer = File_Methods.new()
        writer.txt_file_filer(npc, cechy)
        npc.close()
        writer.rb_file_filer(npc_rb, cechy)
        npc_rb.close()
    end
    erb :show_new_NPC
end

get '/show' do
    @test = []
    @npc = ["plik"]
    Dir.chdir("public/NPC") do
        @npc = Dir.glob("*.rb")
        @npc.each do |element| 
            require("./#{element}")
            class_name = Object.const_get(element.capitalize[/^[^.]*/])
            a = class_name.new()
            @test.push("To jest imie: #{a.nazwa}, a to nazwisko: #{a.nazwisko}, a to ogien #{a.ogien}")
        end
    end
    erb :show_NPC
end
  class Foo

    attr_accessor :nazwa, :nazwisko

    def initialize

    @nazwa = Foo
    @nazwisko = moreFoo

    end

end
<html>
    <head>
        <tile>Show</title>
        <meta charset="UTF-8">
        <link type="text/css" rel="stylesheet" href="CSS/styles.css"/>
    </head>
    <body>
        <h1>Hello </h1>
        <h3> This is the test array >> <%= @test %> </h3>
        <a href="/">Strona Glowna </a>
    </body>
</html>
这些值是从views/new.erb中的POST获得的

<html>
    <head>
        <tile>Tworzenie Postaci</title>
        <meta charset="UTF-8">
        <link type="text/css" rel="stylesheet" href="CSS/styles.css"/>
    </head>
    <body>
        <h1>Kto to bedzie?</h1>
        <form action="/new" method="POST">
            <pre>
                Imie:      <input type="text" name="name"><br/>
                Rodzina:   <input type="text" name="family"><br/>
                Profrsja:  <input type="text" name="class"><br/>
                Ogien:     <input type="text" name="fire"><br/>
                Powietrze: <input type="text" name="air"><br/>
                Ziemia:    <input type="text" name="earth"><br/>
                Woda:      <input type="text" name="water"><br/>
                Pustka:    <input type="text" name="void"><br/>
                <input type="submit">
            </pre>
        </form>
    </body>
</html>

Tworzenie Postaci
去贝齐?
Imie:
罗吉娜:
教授:
奥金:
波维特泽:
齐米亚:
沃达:
普什特卡:
当我运行代码而不是得到Foo,moreFoo时,我什么也得不到,就像初始化变量“nil”一样。为什么?


文件按预期被请求,然后加载所有初始化变量,而不是返回值,它返回nil

问题在于生成.rb文件的方法,它产生了两个错误:

  • 在attr_访问器符号列表末尾创建的循环“”
  • 名称where not string,there for不能作为字符串传递
  • 以下是一种有效的新方法:


    什么是
    moreFoo
    ,它是在哪里定义的?moreFoo是foo.rb文件的初始化变量,就像foo你说的
    @nazwisko=moreFoo
    。但是
    moreFoo
    没有定义两者都应该是字符串如果您只需要字符串,在Foo的初始化中,您可以执行
    @nazwa='Foo'@nazwisko='moreFoo'