Ruby yield and block and Iterators
来源:互联网 发布:公安情报大数据 编辑:程序博客网 时间:2024/05/01 21:10
The key word 'yield' is the core of Iterators, So I use a Iterators to clarify the 'yield'
class Sequence include Enumerable def initialize(from, to, by) @from, @to, @by = from, to, by end def each x = @from y = @by while x<= @to yield x #We will do something to the x, but not now, the operation will ensure in the following block. x += @by end endendWe have use the 'yield' to possess the code line, but have not define the actual operationThen, we can use block to take the place!
s = Sequence.new(1, 10 , 2)s.each {|a| print a} #The |a| means use 'a' to alias the 'x''s place, then use 'print' to take the 'yield''s placeSo the result is:
13579
If we use bellow codes
s = Sequence.new(1, 10 , 2)s.each {print 1} #Nothing will alias to x, but the 'print 1' will take the 'yield''s place
So, the result would be:11111
- Ruby yield and block and Iterators
- Amazing ruby: block & yield
- Matt Austern : Defining Iterators and Const Iterators
- Ruby中yield和block的用法
- ruby 的yield和block研究
- generator and yield
- coroutine and yield
- Generator and yield
- Python yield and generator
- Python yield and iterables
- yield and Generators
- block and static block
- Chapter 8 Visitation: Iterators and Containers
- Extended STL, Volume 1: Collections and Iterators
- Chapter 7Iterators and the Generic for
- Iterables, Iterators and Generators: Part 2
- C# IEnumerator, IEnumerable, and Yield
- ruby and ruby on rails
- C#实现 获取指定字节长度 中英文混合字符串 的方法
- MySQL engines MyISAM and InnoDB
- ASP.NET的错误处理机制
- Notification与NotificationManager通知功能应用
- 总结struts2 iterator status的用法
- Ruby yield and block and Iterators
- dtree来实现树形的菜单
- 求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
- Pyqt4
- Jquery 应用系列:创建复杂表格
- operator=相关思考
- Sqlite3
- Python child, parent, super CLASS
- sqlite3 modify the column