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

时间:2018-03-01 22:20:01

标签: ruby

这是我现在拥有的;唯一的问题是外部文件加载 没有 更新占位符文本 - 而是占位符文本只是说' [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

1 个答案:

答案 0 :(得分: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方法以选择随机字词。