我有一个代码,它会生成一个文件。我希望能够要求初始化变量。
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
LIB /公共/ NPC / foo.rb
class Foo
attr_accessor :nazwa, :nazwisko
def initialize
@nazwa = Foo
@nazwisko = moreFoo
end
end
LIB /视图/ show.erb
<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>
该文件由methods.rb中的方法创建,该方法从main.rb获取attr_hash
def rb_file_filer(name, attr_hash)
imie = attr_hash[:nazwa]
name.write("class #{imie.capitalize}\n")
name.write("attr_accessor")
attr_hash.each { |key, value| name.write(":#{key}, ") }
name.write("\n")
name.write("def initialize\n")
attr_hash.each { |key, value| name.write("@#{key} = #{value}\n") }
name.write("end\n")
name.write("end")
end
这些值来自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>
当我运行代码而不是获取Foo,moreFoo时,我得到的只是初始化变量'nil'。为什么呢?
按预期请求文件,然后加载所有初始化变量而不是返回它返回nil的值。
答案 0 :(得分:0)
问题在于生成.rb文件的方法,它产生了两个错误:
这是一种有效的新方法:
def rb_file_filer(name, attr_hash)
imie = attr_hash[:nazwa]
name.write("class #{imie.capitalize}\n")
name.write("attr_accessor")
attr_hash.each_with_index do |(key, value), index|
if index == attr_hash.length - 1
name.write(":#{key}")
else
name.write(":#{key}, ")
end
end
name.write("\n")
name.write("def initialize\n")
attr_hash.each { |key, value| name.write("@#{key} = '#{value}'\n") }
name.write("end\n")
name.write("end")
end