内容简介:翻译自:https://stackoverflow.com/questions/1751186/impressing-ruby-example
我强烈建议使用.each,.inject和/或.collect.例如:
# Sum 1,3,5,7,9,11,13
[1,3,5,7,11,13].inject { |a,b| a+b }
要么
# Print out all of the files in a directory
Dir.glob('./my_cool_directory/*').each do |file|
puts file
end
要么
# Find the length of all of the strings
["test", "hello there", "how's life today?"].collect{ |string| string.length }
翻译自:https://stackoverflow.com/questions/1751186/impressing-ruby-example
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。