shitou's blog 
Home About Feed | MIX BOY 塌客

Tags "function"

25
Sep

ruby中的几个方法学习 by shitou

0

总结几个ruby中的方法

 

Kernel#test

 

偶然遇到的,曾经看过的几本书中都没有提到,非常好用, 尤其用在system manage方面

>test(?C, 'a.txt') #返回文件a.txt最近的变动时间

>test(:d, 'a.txt') #返回a.txt是不是目录

 

支持很多操作,非常类似于shell中的test(即[]), 不过功能更多,更多的操作符查看ri文档

 

bm

 

bm是ruby内置gem benchmark的方法,作用就是benchmark(性能测试), 使用前需要先调入

>require 'benchmark'

>include Benchmark

>a = (1..1000000).map { rand(1000000) }

>bm do |b|

> b.report("Sort") { a.sort }

> b.report("Sort by") { a.sort_by { |a| a } }

>end

 

report中的参数用于输出结果中的标签(label)

可以用来测试代码的实行效率,优化时使用

 

Enumerable#inject

 

用于迭代, 每次迭代将返回运算结果, 下一次迭代将跳过之前云算过的项,把上一次运算结果作为起始参数(initial)用于下次迭代, 感觉比较绕口,还是看例子吧

>(5..10).inject { |initial, n| initial + n } #45

>(5..10).inject(1) { |initial, n| initial + n } #46

上面的第一个例子没有赋予初始值,那么就开始运算5 + 6, 将结果赋予initial, 然后进行11 + 7, 依次计算

第二个initial = 1, 所以结果为46

 

一个复杂点的例子

找出最长的字符串

>%W(apple pear banane).inject do |x, y|

> x.length > y.length ? x : y

>end #"banana"

找出最长字符串的长度

>%W(apple pear banane).inject(0) do |x, y|

> x >= y.length ? x : y.length

>end #6

 

sort, sort_by

 

sort_by接受一个参数作为block参数, sort可以不跟block或者传递两个参数给block

>%W(apple pear banane).sort

>%W(apple pear banane).sort_by { |word| word.length }

>%W(apple pear banane).sort { |x, y| x.length <=> y.length }

上面第二个和第三个效果相同

 

复杂点的例子

将当前目录下的文件按照修改时间排序

>files = Dir["*"]

>files.sort { |a, b| File.new(a).mtime <=> File.new(b).mtime }

 

Tags: function

2008-09-25 14:46:28, 359 reviews

send to mailbox

Your email:

Tags

U-ka saegusa IN db command Mai Kuraki Norah Jones log iPhoneException ACG Mac Safari objective-c CouchDB LVS AJAX debian 推荐 AMQP google mail bug gettext Erlang 北京 iptables 架构 tips mysql backup function 我看 postfix 监控 SEO cache Etag memcache thread 进程 线程 无锡 yield file column mixboy xml rss gems ruby shitou shell lighttpd 安全 csrf 公司 nginx linux 模块 apache webserver 朋友 大学 生活 尼古拉斯凯奇 movie 文件同步 笑笑 歌词 auto complete plugin rails music ubuntu blog

Category

  • iPhone[17]
  • Erlang[4]
  • google[8]
  • 生活[38]
  • 音乐[11]
  • 电影[11]
  • linux[20]
  • web server[6]
  • mail server[3]
  • cluster[1]
  • system manage[5]
  • ruby[18]
  • ruby on rails[27]
  • 开源[3]

Episode

  • iPhoneException
  • shell
  • thread
  • memcache

Recent Comments

  • comment2, xenical canada, 5528, prilosec packag...
  • comment3, nolvadex package insert, 2877, xenica...
  • comment2, http://www.freecodesource.com/user/pr...
  • comment6, http://www.freecodesource.com/user/pr...
  • comment3, xenical support, :'-), prilosec half ...
  • comment7, how many hydrocodone does it take to ...
  • comment3, oxycodone acetaminophen, :E, viagra d...
  • comment7, http://www.freecodesource.com/user/pr...
  • comment6, valium for pain, accompanying, xanax ...
  • comment4, oxycodone ingredients, 2732, buy viag...

Popular Posts

  • MySQL Innodb备份
  • 准备开始学习Erlang了(恶狼, 二郎..)
  • Lighttpd配置参数
  • iPhone上的HelloWorld终于跑起来了
  • Etag和Expire

Recommended Posts

  • Mai Kuraki -永远より ながく
  • U-ka saegusa IN db Final Best
  • Heaven Can Wait - Charlotte Gainsbou
  • Ruby遍历MemCached的key
  • Norah Jones - The fall
  • 请记得仰望梦想的姿势
  • Shell: 统计MySQL InnoDB表的大小
  • Rails Benchmark
  • 发送异常到邮箱
  • I Miss Nobody
  • Music4u, Vol. 1
  • my macbook
  • Mai Kuraki-Beautiful
  • 10首最伤情英文歌曲精选
  • Mai Kuraki - PUZZLE/Revive

Friends' blogs

  • levy
  • sphance
  • andreas

Login

   注册

留言 查看留言

留言

   取消

留言 查看留言


Statistics

  • 访问次数: 51169
  • 今天访问: 33
  • 日志: 172
  • 评论: 117
  • 音乐: 9
  • 用户: 149


 

just DO NOT support IE

close