双“gsub”多变的

发布于 2024-12-16 21:30:37 字数 325 浏览 2 评论 0原文

是否可以在 gsub 方法的两个字段中使用变量? 我正在尝试让这段代码工作:

$I = 0
def random_image
  $I.to_s
  random = rand(1).to_s
  logo = File.read('logo-standart.txt')
  logo_aleatoire = logo.gsub(/#{$I}/, random)
  File.open('logo-standart.txt', "w") {|file| File.puts logo_aleatoire}
  $I.to_i
  $I += 1
end

提前致谢!

Is it possible to use variables in both fields of the gsub method ?
I'm trying to get this piece of code work :

$I = 0
def random_image
  $I.to_s
  random = rand(1).to_s
  logo = File.read('logo-standart.txt')
  logo_aleatoire = logo.gsub(/#{$I}/, random)
  File.open('logo-standart.txt', "w") {|file| File.puts logo_aleatoire}
  $I.to_i
  $I += 1
end

Thanks in advance !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆追雨的时光 2024-12-23 21:30:37
filecontents = File.read('logo-standart.txt')
filecontents.gsub!(/\d+/){rand(100)}
File.open("logo-standart.txt","w"){|f| f << filecontents }

神奇的线是第二线。

  • gsub! 函数会就地修改字符串,这与 gsub 函数不同,后者会返回一个新字符串并保持第一个字符串不变。
  • 我传递给 gsub! 的单个参数是要匹配的模式。在这里,目标是匹配任何一个或多个数字的字符串 - 这是您要替换的数字。无需循环遍历在每个数字上运行 gsub 的所有可能数字。您甚至可以匹配高达 googol(或更高)的数字,而无需您的程序运行越来越长的时间。
  • 每次模式匹配时都会评估 gsub! 所采用的块,以编程方式生成替换编号。所以每次,你都会得到一个不同的随机数。这与采用两个参数的更常见的 gsub! 形式不同——在任何模式匹配发生之前,该参数会被计算一次,并且所有匹配都将替换为相同的字符串。

请注意,这种结构的方式是,每次比赛您都会获得一个新的随机数。所以如果数字307出现两次,它就会变成两个不同的随机数。

如果您想每次将 307 映射到相同的随机数,您可以执行以下操作:

filecontents = File.read('logo-standart.txt')
randomnumbers = Hash.new{|h,k| h[k]=rand(100)}
filecontents.gsub!(/\d+/){|match| randomnumbers[match]}
File.open("logo-standart.txt","w"){|f| f << filecontents }

这里,randomnumbers 是一个散列,可让您查找数字并找到它们对应的随机数。构造哈希时传递的块告诉哈希在找到以前从未见过的数字时要做什么——在这种情况下,生成一个新的随机数,并记住该随机数的映射。因此,gsub! 的块只是要求哈希为其映射数字,而 randomnumbers 会在您遇到原始文件中的新数字时负责生成新的随机数。

filecontents = File.read('logo-standart.txt')
filecontents.gsub!(/\d+/){rand(100)}
File.open("logo-standart.txt","w"){|f| f << filecontents }

The magic line is the second line.

  • The gsub! function modifies the string in-place, unlike the gsub function, which would return a new string and leave the first string unmodified.
  • The single parameter that I passed to gsub! is the pattern to match. Here, the goal is to match any string of one or more digits -- this is the number that you're going to replace. There's no need to loop through all of the possible numbers running gsub on each one. You can even match numbers as high as a googol (or higher) without your program taking longer and longer to run.
  • The block that gsub! takes is evaluated each time the pattern matches to programmatically generate a replacement number. So each time, you get a different random number. This is different from the more usual form of gsub! that takes two parameters -- there the parameter is evaluated once before any pattern matching occurs, and all matches are replaced by the same string.

Note that the way this is structured, you get a new random number for each match. So if the number 307 appears twice, it turns into two different random numbers.

If you wanted to map 307 to the same random number each time, you could do the following:

filecontents = File.read('logo-standart.txt')
randomnumbers = Hash.new{|h,k| h[k]=rand(100)}
filecontents.gsub!(/\d+/){|match| randomnumbers[match]}
File.open("logo-standart.txt","w"){|f| f << filecontents }

Here, randomnumbers is a hash that lets you look up the numbers and find what random number they correspond to. The block passed when constructing the hash tells the hash what to do when it finds a number that it hasn't seen before -- in this case, generate a new random number, and remember what that random number the mapping. So gsub!'s block just asks the hash to map numbers for it, and randomnumbers takes care of generating a new random number when you encounter a new number from the original file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文