英文:
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 = ["bring", "finger", "drought", "singing", "bingo", "purposeful"]
new_word = []
# Your code goes here
words.find_all do |word|
if word.last(3) == "ing"
new_word << word
end
end
new_word
# expected return value is ["bring", "singing"]
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' for "bring":String (NoMethodError)
if word.last(3) == "ing"
^^^^
答案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.
["bring", "finger", "drought", "singing", "bingo", "purposeful"].select { |s|
s.end_with? "ing"
}
When executing your code, we see the following error.
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)
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
.
["bring", "finger", "drought", "singing", "bingo", "purposeful"].select { |s|
s.rstrip.end_with? "ing"
}
And if you don't care about case, then you can either lowercase the string:
["bring", "WING", "finger", "drought", "singing", "bingo", "purposeful"].select { |s|
s.rstrip.downcase.end_with? "ing"
}
Or use a regular expression:
["bring", "WING", "finger", "drought", "singing", "bingo", "purposeful"].select { |s|
s =~ /ing\s*\Z/i
}
Or using the otherwise mentioned #grep
:
["bring", "WING", "finger", "drought", "singing", "bingo", "purposeful"].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? "ing"
end
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论