【问题标题】:How to replace text in a ruby string如何替换红宝石字符串中的文本
【发布时间】:2013-12-28 10:10:49
【问题描述】:

我正在尝试在 Ruby 中编写一个非常简单的方法,该方法接受一个字符串和一个单词数组,并检查字符串是否包含任何单词,如果包含,则将它们替换为大写。

我尝试了,但由于我的 Ruby 技能水平,它不是很好。

def(my_words,my_sentence)
  #split the sentence up into an array of words
  my_sentence_words =  my_sentence.split(/\W+/)
  #nested loop that checks the words array for each brand 
  my_sentence_words.each do |i|
    my_words.each do |i|
      #if it finds a brand in the words and sets them to be uppercase
      if my_words[i] == my_sentence_words[i]
        my_sentence_words[i] == my_sentence_words[i].up.case
      end
    end
  end

  #put the words array into one string
  words.each do |i|
    new_sentence = ("" + my_sentence_words[i]) + " "
  end
end

我得到:can't convert string into integer error

【问题讨论】:

  • 一件事你使用i 两次,内部循环使用j。其次,您没有给函数命名。列出完整的错误。
  • 提供有意义的示例数据
  • “有关您编写的代码问题的问题必须在问题本身中描述具体问题 - 并包含重现问题的有效代码。有关指导,请参阅 SSCCE.org。”
  • 当您获得使用 Ruby 的经验时,您会发现循环索引的频率几乎不像您习惯使用 C 等那样频繁。您将更多地依赖each 和Enumerable“mixin”(模块)中的众多方法,几乎​​所有这些方法都是用each 实现的。

标签: ruby arrays string loops


【解决方案1】:
def convert(mywords,sentence)
 regex = /#{mywords.join("|")}/i
 sentence.gsub(regex) { |m| m.upcase }
end
convert(%W{ john james jane }, "I like jane but prefer john")
#=> "I like JANE but prefer JOHN"

【讨论】:

  • 请注意 convert(%W{ JANE }, "I like JANE") => "I like " 和 convert(%W{ JANE }, "I like jane") => "I like jane"。您需要添加/i 并删除!。
  • 小心/#{mywords.join("|")}/i。它将创建一个允许子字符串匹配的模式,而不是整个单词匹配。 /\b#{mywords.join("|")}\b/i 会更好。
【解决方案2】:

这样会更好。它遍历品牌,搜索每个品牌,并替换为大写版本。

brands = %w(sony toshiba)
sentence = "This is a sony. This is a toshiba."

brands.each do |brand|
  sentence.gsub!(/#{brand}/i, brand.upcase)
end

字符串中的结果。

"This is a SONY. This is a TOSHIBA."

献给喜欢 Ruby foo 的人!

sentence.gsub!(/#{brands.join('|')}/i) { |b| b.upcase }

在函数中

def capitalize_brands(brands, sentence)
  sentence.gsub(/#{brands.join('|')}/i) { |b| b.upcase }
end

【讨论】:

  • 谢谢,还有另一个问题,例如,如果我在我的 rails 应用程序中使用此代码,并且想要获取一个字符串并用指向 url 的链接替换 ​​wordd,那么它将转到brands/sony等等。我怎么能把一个链接放在一个字符串里面?
  • 只要使用link_to b.upcase, 'YOUR_URL'
  • 笨蛋,link_to 需要 require 吗?
  • 如果你在助手中使用它不应该。
  • 它似乎来自ActionView::Helpers,我认为它与 Rails 一起使用。这就是我对require 的意思,因为这个问题没有用 Rails 标记。
【解决方案3】:

您收到此错误是因为i 并非如您预期的那样从0 开始,在each 方法中i 是数组元素,并且具有字符串类型,它是您句子中的第一个单词:

my_sentence_words = ["word"]
my_sentence_words.each do |i|
  puts i.length #=> 4
  puts i.type   #=> String
  puts i        #=> word
end

所以你尝试调用my_sentence_words[word] 而不是my_sentence_words[0]。您可以尝试通过 index 的元素而不是元素本身的方法 each_index :

def check(str, *arr)
  upstr = str.split(' ')
  upstr.eachindex do |i|       #=> i is index
    arr.each_index do  |j|  
      upstr[i].upcase! if upstr[i] == arr[j]        
    end
  end
  upstr
end

check("This is my sentence", "day", "is", "goal", "may", "my")
#=>["This", "IS", "MY", "sentence"]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-23
    • 2010-11-29
    • 1970-01-01
    • 2015-08-13
    • 1970-01-01
    • 2014-02-11
    • 2011-01-21
    • 2022-01-21
    相关资源
    最近更新 更多