ruby - 如何在字符串中搜索最长的单词
问题描述
def my_inject(*args)
return yield false if args.empty? && !block_given?
case args.length
when 1 then args.first.is_a?(Symbol) ? sym = args.first : result = args.first
when 2 then result = args.first
sym = args.last
end
result ||= 0
my_each { |x| result = block_given? ? yield(result, x) : result.send(sym, x) }
result
end
我可以在此代码中添加什么以使其在字符串数组中搜索最长的单词并添加它?
解决方案
string.split(" ")
.max_by(&:length)
推荐阅读
- android - 货币获取符号没有为美元,android提供正确的符号?
- typescript - 如何从数组中过滤多条记录?
- c# - c# Android 如何将 Enum 放入包中
- java - 无法理解 Thread join()
- .net-core - SonarScanner 可以在 linux 上运行但没有结果?(.NET 核心 2)
- algorithm - 最大唯一连续子序列
- mysql - 如何在node.js中实现一个promise代码片段代替Mysql客户端
- python - 如何将 Kivy 画布转换为 SVG?
- regex - Regex 正则表达式最小字符数
- solr - 如何在 solr schema.xml 中读取 DECIMAL 数据类型:audit_internal_id decimal(38,10)