find all words ending in "ing" in ruby

huangapple go评论43阅读模式
英文:

find all words ending in "ing" in ruby

问题

def find_all_words_ending_in_ing
  words = ["bring", "finger", "drought", "singing", "bingo", "purposeful"]
  new_word = []
  # 你的代码放在这里
  words.find_all do |word|
    if word[-3..-1] == "ing"
      new_word << word
    end
  end
  new_word
  # 预期返回值是 ["bring", "singing"]
end

错误信息说明String类没有last方法,可以使用[-3..-1]来获取末尾的三个字符。

英文:
def find_all_words_ending_in_ing
  words = [&quot;bring&quot;, &quot;finger&quot;, &quot;drought&quot;, &quot;singing&quot;, &quot;bingo&quot;, &quot;purposeful&quot;]
  new_word = []
  # Your code goes here
  words.find_all do |word|
    if word.last(3) == &quot;ing&quot;
      new_word &lt;&lt; word
    end
  end
  new_word
  #  expected return value is [&quot;bring&quot;, &quot;singing&quot;]
end

The code needs to iterate through the items in the array and output the words ending with "ing"

I get this error:

undefined method `last&#39; for &quot;bring&quot;:String (NoMethodError)
      if word.last(3) == &quot;ing&quot;
              ^^^^

答案1

得分: 5

我建议你使用 #select#end_with? 来代替。

["bring", "finger", "drought", "singing", "bingo", "purposeful"].select { |s| 
  s.end_with? "ing" 
}

当执行你的代码时,我们看到以下错误。

Traceback (most recent call last):
        8: from /usr/local/bin/irb:23:in `<main>'
        7: from /usr/local/bin/irb:23:in `load'
        6: from /var/lib/gems/2.5.0/gems/irb-1.2.0/exe/irb:11:in `<top (required)>'
        5: from (irb):31
        4: from (irb):23:in `find_all_words_ending_in_ing'
        3: from (irb):23:in `find_all'
        2: from (irb):23:in `each'
        1: from (irb):24:in `block in find_all_words_ending_in_ing'
NoMethodError (undefined method `last' for "bring":String)

如果你查看字符串类的文档,你会发现没有 #last 方法,这正是错误所告诉你的。

然而,#end_with? 会接受一个字符串并检查该字符串是否是你调用该方法的字符串的后缀。

如果要忽略数组中字符串的尾部空格,你可以使用 #rstrip

["bring", "finger", "drought", "singing", "bingo", "purposeful"].select { |s| 
  s.rstrip.end_with? "ing" 
}

如果你不关心大小写,那么你可以将字符串转换为小写:

["bring", "WING", "finger", "drought", "singing", "bingo", "purposeful"].select { |s| 
  s.rstrip.downcase.end_with? "ing" 
}

或者使用正则表达式:

["bring", "WING", "finger", "drought", "singing", "bingo", "purposeful"].select { |s| 
  s =~ /ing\s*\Z/i
}

或者使用之前提到的 #grep

["bring", "WING", "finger", "drought", "singing", "bingo", "purposeful"].grep(/ing\s*\Z/i)
英文:

I would suggest you use #select and #end_with? instead.

[&quot;bring&quot;, &quot;finger&quot;, &quot;drought&quot;, &quot;singing&quot;, &quot;bingo&quot;, &quot;purposeful&quot;].select { |s| 
  s.end_with? &quot;ing&quot; 
}

When executing your code, we see the following error.

Traceback (most recent call last):
        8: from /usr/local/bin/irb:23:in `&lt;main&gt;&#39;
        7: from /usr/local/bin/irb:23:in `load&#39;
        6: from /var/lib/gems/2.5.0/gems/irb-1.2.0/exe/irb:11:in `&lt;top (required)&gt;&#39;
        5: from (irb):31
        4: from (irb):23:in `find_all_words_ending_in_ing&#39;
        3: from (irb):23:in `find_all&#39;
        2: from (irb):23:in `each&#39;
        1: from (irb):24:in `block in find_all_words_ending_in_ing&#39;
NoMethodError (undefined method `last&#39; for &quot;bring&quot;:String)

If you look at the documentation for the String class, there is not a #last method, which is exactly what the error is telling you.

However, #end_with? will take a string and check if that string is a suffix of the string you're calling the method on.

To disregard any trailing whitespace in the strings in your array, you may wish to use #rstrip.

[&quot;bring&quot;, &quot;finger&quot;, &quot;drought&quot;, &quot;singing&quot;, &quot;bingo&quot;, &quot;purposeful&quot;].select { |s| 
  s.rstrip.end_with? &quot;ing&quot; 
}

And if you don't care about case, then you can either lowercase the string:

[&quot;bring&quot;, &quot;WING&quot;, &quot;finger&quot;, &quot;drought&quot;, &quot;singing&quot;, &quot;bingo&quot;, &quot;purposeful&quot;].select { |s| 
  s.rstrip.downcase.end_with? &quot;ing&quot; 
}

Or use a regular expression:

[&quot;bring&quot;, &quot;WING&quot;, &quot;finger&quot;, &quot;drought&quot;, &quot;singing&quot;, &quot;bingo&quot;, &quot;purposeful&quot;].select { |s| 
  s =~ /ing\s*\Z/i
}

Or using the otherwise mentioned #grep:

[&quot;bring&quot;, &quot;WING&quot;, &quot;finger&quot;, &quot;drought&quot;, &quot;singing&quot;, &quot;bingo&quot;, &quot;purposeful&quot;].grep(/ing\s*\Z/i)

答案2

得分: 4

如果你不怕正则表达式,有一个名为 grep 的强大工具可以完成这个任务,它的工作方式很像同名的命令行工具:

words.grep(/ing\z/)

在这里,\z 在Ruby正则表达式中表示"字符串的末尾"。

--

它不仅匹配字符串,还利用了 === 运算符,所以比你想象的更方便!

英文:

If you're not afraid of regular expressions, there's a great tool for this job called grep which works a lot like<sup>1</sup> the command-line tool of the same name:

words.grep(/ing\z/)

Here \z means "end of string" in Ruby regexp notation.

--

<sup>1</sup> It also does way more than match strings as it leverages the === operator, so it is even more handy than you might think!

答案3

得分: 1

String没有last()方法,但String有end_with?方法。

示例:

words.select do |word|
  word.end_with? "ing"
end
英文:

String has no last() method but
String has end_with? method.

Example:

words.select do |word|
  word.end_with? &quot;ing&quot;
end

huangapple
  • 本文由 发表于 2023年5月15日 01:19:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定