这是怎么开始的:
def first_words(s, n)
a = s.split(/\s/) # or /[ ]+/ to only split on spaces
a[0...n].join(' ') + (a.size > n ? '...' : '')
end
s = "The quick brown fox jumps over the lazy dog. " * 20
puts "#{s.size}, #{s.split(/\s/).size}"
#-> 900, 180
puts first_words(s, 10)
#-> The quick brown fox jumps over the lazy dog. The...
puts first_words("a b c d", 10)
#-> a b c d
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
732 次 |
| 最近记录: |