Understanding Ruby blocks, Procs and methods

写的好,我收藏起来慢慢看. 写的好,我支持一下. 没什么意思,把他踩下去. 该文章被关注了358次,收藏了0次,顶了0次 ,踩了0脚.

Ruby中的blocks, Procs 和 methods很是博大精深,我至今不得要领,每次看到文章,都想给大家分享。

刚才G.zhen.ning给我推荐了一篇文章,写的很不错,建议大家看看,地址如下:
http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods


Ruby provides the programmer with a set of very powerful features borrowed from the domain of functional programming, namely closures, high-order functions and first-class functions [1]. These features are implemented in Ruby by means of code blocks, Proc objects and methods (that are also objects) – concepts that are closely related and yet differ in subtle ways. In fact I found myself quite confused about this topic, having a difficulty to understand the difference between blocks, procs and methods and unsure about the best practices of using them. Additionally, having some background in Lisp and years of Perl experience, I was unsure of how the Ruby concepts map to similar idioms from other programming languages, like Lisp’s functions and Perl’s subroutines. Sifting through hundreds of newsgroup posts, I saw that I’m not the only one with this problem, and in fact quite a lot of “Ruby Nubies” struggle with the same ideas.



In this article I lay out my understanding of this facet of Ruby, which comes as a result of extensive research of Ruby books, documentation and comp.lang.ruby, in sincere hope that other people will find it useful as well.


另外,技能云以前也有一篇文章Proc.new vs Lambda in Ruby 供参考。

标签: ruby, blocks, Procs, methods
发布时间: 2008-01-23 11:28:02 | 发布者: IceskYsl | 阅读次数:358 回到主页

登录发表你对这个话题的看法吧?



(在注册时提供的email地址)



(注册时设定的密码)
(公共场合请慎用!)

忘记密码了? 还没有注册?