印象深刻的Ruby例子

栏目: Ruby · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/1751186/impressing-ruby-example
在某些日子里,我正在大学谈论一个Rails项目,我想向 Ruby

介绍观众.

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

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

最好的祝福

我强烈建议使用.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


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

High Performance Python

High Performance Python

Andrew Lewis / O'Reilly Media, Inc. / 2010-09-15 / USD 34.99

Chapter 1. Introduction Section 1.1. The High Performance Buzz-word Chapter 2. The Theory of Computation Section 2.1. Introduction Section 2.2. Problems Section 2.3. Models of Computati......一起来看看 《High Performance Python》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具