Ruby 数组中最长的单词
我构建这个方法是为了查找数组中最长的单词,但我想知道是否有更好的方法来完成它。我对 Ruby 还很陌生,只是将其作为学习 inject
方法的练习。
它返回数组中最长的单词,或相等最长单词的数组。
class Array
def longest_word
# Convert array elements to strings in the event that they're not.
test_array = self.collect { |e| e.to_s }
test_array.inject() do |word, comparison|
if word.kind_of?(Array) then
if word[0].length == comparison.length then
word << comparison
else
word[0].length > comparison.length ? word : comparison
end
else
# If words are equal, they are pushed into an array
if word.length == comparison.length then
the_words = Array.new
the_words << word
the_words << comparison
else
word.length > comparison.length ? word : comparison
end
end
end
end
end
I built this method to find the longest word in an array, but I'm wondering if there's a better way to have done it. I'm pretty new to Ruby, and just did this as an exercise for learning the inject
method.
It returns either the longest word in an array, or an array of the equal longest words.
class Array
def longest_word
# Convert array elements to strings in the event that they're not.
test_array = self.collect { |e| e.to_s }
test_array.inject() do |word, comparison|
if word.kind_of?(Array) then
if word[0].length == comparison.length then
word << comparison
else
word[0].length > comparison.length ? word : comparison
end
else
# If words are equal, they are pushed into an array
if word.length == comparison.length then
the_words = Array.new
the_words << word
the_words << comparison
else
word.length > comparison.length ? word : comparison
end
end
end
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我会做
I would do
Ruby 有一个标准方法,用于返回列表中具有最大值的元素。
或者可以使用 max_by 方法
获取所有最大长度的元素
Ruby has a standard method for returning an element in a list with the maximum of a value.
or you can use the max_by method
to get all the elements with the maximum length
这是使用
inject
的一个(不适用于空数组):可以将其缩短为。
对于那些喜欢高尔夫的人来说,
Here's one using
inject
(doesn't work for an empty array):which can be shortened to
for those who like golf.
两行:
或者如果你想使用注入,这使用你的想法,但它更短。
A two liner:
or if you want use inject, this use your idea, but its more short.
此方法仅依赖于通用的
Enumerable
方法,没有任何Array
特定的内容,因此我们可以将其拉入Enumerable
模块,其中它也可用于Set
或Enumerator
,而不仅仅是Array
。This method only depends on generic
Enumerable
methods, there's nothingArray
specific about it, therefore we can pull it up into theEnumerable
module, where it will also be available forSet
s orEnumerator
s, not justArray
s.该解决方案使用注入方法累积数组中最长的字符串,然后选择长度最大的字符串。
动物 = ["老鼠", "猫", "鸟", "熊", "驼鹿"]
Animals.inject(Hash.new{|h,k| h[k] = []}) { |acc, e | acc[e.size] << e; acc }.sort.last[1]
这将返回:
[“鼠标”,“鼠标”]
This solution uses the inject method to accumulate the longest strings in an array, then picks the ones with the highest length.
animals = ["mouse", "cat", "bird", "bear", "moose"]
animals.inject(Hash.new{|h,k| h[k] = []}) { |acc, e| acc[e.size] << e; acc }.sort.last[1]
This returns:
["mouse", "mouse"]