【问题标题】:Interacting with array elements in Ruby在 Ruby 中与数组元素交互
【发布时间】:2014-12-12 08:55:46
【问题描述】:

我正在尝试用我的代码做一些事情,但我对如何与数组的元素进行交互感到困惑。这段代码的重点是将字符串作为输入并编辑某些单词。

我的目标是:

  1. 让我的程序编辑多个单词。

  2. 创建一个新的编辑字符串并将其保存为变量,而不是仅仅将其打印到控制台

我已经使用 cmets 概述了我的逻辑(cmets 中也有几个问题)。

puts "Write something: "

text = gets.chomp

puts "Redact some words: "

redact = gets.chomp

words = text.split(" ")  #Turns string into array of words

redact_words = text.split(" ")

words.each do |word|        #Iterates through each component of array [words] and permanently  
word.downcase!              #makes the placeholder (word) lower case.
 end                        #Apparently this works also: words.map!(&:downcase) to make it
                              #undercase but I'm not sure why? What is .map? What is &:?

words.each do |word|

if redact_words.include?(word)   #iterates through each component of array [redact_words] and 
                                 #checks if it includes the placeholder (word). If it does,
text.gsub!("#{word}","REDACTED") # it permanently globally substitutes the string of placeholder
                                 #(word) with the string "REDACTED".
else puts "No redactable word(s) found"
end
end
puts text

我的代码运行得不是很好,因为它没有看到大小写。

如果您输入“Hello hello Okay OK”作为第一个输入。并且“你好,好吧”作为第二个输入。你会得到“Hello REDACTED Okay REDACTED”作为输出。

认为 是因为它打印修改后的字符串文本而不是数组 [words](这是我想要的,因为我希望将编辑后的字符串保存到变量中)。有没有其他方法可以做到这一点?

另外,除了使用 regexp(正则表达式)之外,有没有办法进行我所做的替换?

【问题讨论】:

  • 应该是words = text.split(" ")redact_words = redact.split(" ")。这是一个错字还是您在实际代码中犯了错误?
  • 那是一个错字。但是由于某种原因,它仍然得到了相同的结果...... O_O
  • 我一直使用 repl.it/languages/Ruby 作为我的 Ruby 解释器。

标签: ruby arrays regex


【解决方案1】:

回答您关于words.map!(&:downcase) 为何有效的问题:

  1. each 一样,map 循环遍历数组中的每一项,并将该项传递给给定的块。不同之处在于它使用返回的项目创建了一个新数组。 map! 将当前数组中的项替换为返回的项,而不是创建一个新数组。 reference
  2. &:downcase{|x| x.downcase } 的快捷方式。它本质上是对数组中的每一项调用指定的方法,并将返回的值作为新值。

这是您使用正则表达式的脚本版本:

puts "Write something: "

text = gets.chomp

puts "Redact some words: "

redact = gets.chomp

redact_words = redact.split(" ")

# The %r syntax creates a regular expression using interpolation, the /i means to ignore case
new_text = text.gsub(%r/\b(#{redact_words.join('|')})\b/i, 'REDACTED')

puts "No redactable word(s) found" if new_text == text

puts new_text

【讨论】:

    【解决方案2】:

    您应该使您的搜索不区分大小写。

    最简单的方法是构建一个正则表达式(使用Regexp.union),然后将其与整个字符串匹配:

    def redact(text, redact)
      redact_words = redact.split(" ")  
      redact_patterns = redact_words.map { |word| /\b#{word}\b/i } # /i makes it case insensitive, 
                                                                   # \b makes it whole word only
      text.gsub(Regexp.union(redact_patterns), 'REDACTED')
    end
    
    redact('Hello hello Okay okay', "hello okay")
    # => "REDACTED REDACTED REDACTED REDACTED"
    

    【讨论】:

      猜你喜欢
      • 2019-04-08
      • 2023-04-11
      • 2021-08-19
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-01
      相关资源
      最近更新 更多