ruby占位符不在外部.txt文件中填充

问题描述 投票:0回答:1

这是我现在拥有的;唯一的问题是外部文件在没有更新占位符文本的情况下加载 - 相反,占位符文本只是说'[NOUN]'而不是在早期程序提示中从用户插入的实际名词。

更新;用@tadmans建议清理,但仍然没有将用户输入传递给外部.txt文件中的占位符文本。

    puts "\n\nOnce upon a time on the internet... \n\n"
    puts "Name 1 website:"
    print "1. "
    loc=gets
    puts "\n\Write 5 adjectives: "
    print "1. "
    adj1=gets
    print "\n2. "
    adj2=gets
    print "\n3. "
    adj3=gets
    print "\n4. "
    adj4=gets
    print "\n5. "
    adj5=gets
    puts "\n\Write 2 nouns: "
    print "1. "
    noun1=gets
    print "\n2. "
    noun2=gets
    puts "\n\nWrite 1 verb: "
    print "1. "
    verb=gets
    puts "\n\nWrite 1 adverb: "
    print "1. "
    ptverb=gets

string_story = File.read("dynamicstory.txt")

puts string_story

目前的输出是(即占位符未填充):

   \n\nOnce upon a time on the internet...\n\n
      One dreary evening while browsing the #{loc} website online, I stumbled accross a #{adj1} Frog creature named Earl. This frog would sit perturbed for hours at a time at the corner of my screen like Malware. One day, the frog appeared with a #{adj2} companion named Waldo that sat on the other corner of my screen.  He had a #{adj3} set of ears with sharp #{noun1} inside.  As the internet frogs began conversing and becoming close friends in hopes of #{noun2}, they eventually created a generic start-up together. They knew their start-up was #{adj4} but didn't seem to care and pushed through anyway. They would #{verb} on the beach with each other in the evenings after operating with shady ethics by day. They could only dream of a shiny and #{adj5} future full of gold. But then they eventually #{ptverb} and moved to Canada.\n\n
      The End\n\n\n
ruby
1个回答
2
投票

值得注意的是,Ruby字符串插值语法仅在实际的Ruby代码中有效,并且不适用于外部文件。那些只是简单的字符串。

如果你想对那些你需要重构你的程序的粗略插值,以便让它变得容易。你想要的最后一件事是必须eval那个字符串。

编写代码时,请始终考虑将程序分解为具有特定功能的方法或函数,并可在各种情况下使用。 Ruby通常鼓励代码重用并推广“DRY原则”,或“不要重复自己”。

例如,您的输入方法归结为此通用方法:

def input(thing, count = 1)
  puts "Name %d %s:" % [ count, thing ]

  count.times.map do |i|
    print '%d. ' % (i + 1)

    gets.chomp
  end
end

在哪里获得具有任意计数的随机事物的输入。我在这里使用sprintf风格的格式化程序与%但你可以自由地使用常规插值,如果这是你喜欢它。我发现它会导致字符串变得不那么混乱,尤其是在插入复杂的代码块时。

接下来,您需要将该数据组织到适当的容器中,以便以编程方式访问它。使用一堆不相关的变量是有问题的。在这里使用Hash可以轻松实现:

puts "\n\nOnce upon a time on the internet... \n\n"

words = { }

words[:website] = input('website')
words[:adjective] = input('adjectives', 5)
words[:noun] = input('nouns', 2)
words[:verb] = input('verb')
words[:adverb] = input('adverb')

请注意您现在可以通过重新排序代码行来改变这些事物的顺序,并且您可以通过调整单个数字来更改您要求的内容的数量,非常简单。

接下来要解决的是插值问题。而不是使用难以评估的Ruby符号#{...},而是使用简单的东西。在这种情况下使用%verb1%noun2

def interpolate(string, values)
  string.gsub(/\%(website|adjective|noun|verb|adverb)(\d+)/) do
    values.dig($1.to_sym, $2.to_i - 1)
  end
end

这看起来有点难看,但正则表达式用于识别那些标签,$1$2根据正则表达式中的捕获分别拉出两个部分,单词和数字。这可能看起来有点高级,但如果你花时间去理解这个方法,你可以很快解决相当复杂的问题。在解析或重写字符串时,您将在很多情况下使用它。

这是测试它的快速方法:

string_story = File.read("dynamicstory.txt")

puts interpolate(string_story, words)

文件内容如下所示:

One dreary evening while browsing the %website1 website online,
I stumbled accross a %adjective1 Frog creature named Earl.

你也可以调整你的interpolate方法来选择随机单词。

© www.soinside.com 2019 - 2024. All rights reserved.