印象深刻的Ruby例子

bra*_*fck 7 ruby

在某些日子里,我正在大学谈论一个Rails项目,我想向Ruby介绍观众.

我想向他们展示一两个非常好的代码示例来演示Ruby是多么棒的.

你知道一个很好的例子吗?

最好的祝福

gra*_*fic 6

向他们展示如何在3个简单易懂的代码中改变50条丑陋的脏代码行.(作为评论的第一行)

不要表现出你对红宝石有多酷.但是如果他们使用红宝石他们会有多酷:)


Top*_*gio 5

我会强烈建议用的东西.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)