我会强烈建议用的东西.each,.inject和/或.collect.例如:
# Sum 1,3,5,7,9,11,13
[1,3,5,7,11,13].inject { |a,b| a+b }
Run Code Online (Sandbox Code Playgroud)
要么
# Print out all of the files in a directory
Dir.glob('./my_cool_directory/*').each do |file|
puts file
end
Run Code Online (Sandbox Code Playgroud)
要么
# Find the length of all of the strings
["test", "hello there", "how's life today?"].collect{ |string| string.length }
Run Code Online (Sandbox Code Playgroud)