在某些日子里,我正在大学谈论一个Rails项目,我想向
Ruby介绍观众.
@H_404_2@我想向他们展示一两个非常好的代码示例来演示Ruby是多么棒的.
@H_404_2@你知道一个很好的例子吗?
@H_404_2@最好的祝福
解决方法
我强烈建议使用.each,.inject和/或.collect.例如:
# Sum 1,3,5,7,9,11,13 [1,13].inject { |a,b| a+b }@H_404_2@要么
# Print out all of the files in a directory Dir.glob('./my_cool_directory/*').each do |file| puts file end@H_404_2@要么
# Find the length of all of the strings ["test","hello there","how's life today?"].collect{ |string| string.length }