Ruby/Rails使用gsub和数组 [英] Ruby/Rails working with gsub and arrays

查看:50
本文介绍了Ruby/Rails使用gsub和数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Ruby中使用gsub方法时要使用的字符串.问题是我有一个动态的字符串数组,需要迭代以搜索原始文本并替换为该字符串.

I have a string that I am trying to work with in using the gsub method in Ruby. The problem is that I have a dynamic array of strings that I need to iterate through to search the original text for and replace with.

例如,如果我有以下原始字符串(这是我正在使用的一些示例文本,希望可以正常使用),并且有一组要搜索和替换的项目.

For example if I have the following original string (This is some sample text that I am working with and will hopefully get it all working) and have an array of items I want to search through and replace.

感谢您的提前帮助!

推荐答案

a = ['This is some sample text',
     'This is some sample text',
     'This is some sample text']

所以a是示例数组,然后遍历该数组并替换值

so a is the example array, and then loop through the array and replace the value

a.each do |s|
    s.gsub!('This is some sample text', 'replacement')
end

这篇关于Ruby/Rails使用gsub和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆