Ruby aus _whys Buch gelernt und ich habe versucht, seinen Code neu zu erstellen, aber es funktioniert nicht.require_relative zieht die Variablen nicht?
Ich habe eine world.rb Datei;
puts "Hello World"
Put_the_kabosh_on = "Put the kabosh on"
code_words = {
starmonkeys: "Phil and Pete, thouse prickly chancellors of the New Reich",
catapult: "Chunky go-go",
firebomb: "Heat-Assisted Living",
Nigeria: "Ny and Jerry's Dry Cleaning (with Donuts)",
Put_the_kabosh_on: "Put the cable box on"
}
Und in meiner anderen Datei, pluto.rb;
require_relative "world"
puts "Hello Pluto"
puts code_words[:starmonkeys]
puts code_words[:catapult]
puts code_words[:firebomb]
puts code_words[:Nigeria]
puts code_words[:Put_the_kabosh_on]
Ich kenne meine require_relative
funktioniert, denn wenn ich pluto.rb ohne Hash-Teil laufen (nur setzt "Hello World"
), wird Hallo Welt gedruckt!
Wenn Sie die code_words sind expectecting lokale Variable in pluto.rb existieren, dann es wird nicht. Dies ist die Art und Weise erfordern (_relative) funktioniert –
Was ist Ihre Frage? – sawa
Innerhalb einer Rails-App sollten Sie diese in eine YAML-Datei einfügen. Werfen Sie einen Blick auf http://guides.rubyonrails.org/i18n.html – Stefan