Created
November 15, 2012 00:06
-
-
Save ilake/4075750 to your computer and use it in GitHub Desktop.
Rubybits2 LEVEL 1: Proc, block, lambda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # & then proc object into block | |
| # & also could then block into a proc object | |
| # & 可以讓proc object 跟 block 切來切去 | |
| # Calling a method with & in front of a parameter | |
| tweets.each(&printer) # turns a proc into block | |
| # Defining a method with & in front of a parameter | |
| def each(&block) # turns a block into a proc, so it can be assigned to parameter | |
| timeline = Timeline.new(tweets) | |
| timeline.each do |tweet| puts tweet | |
| end | |
| class Timeline | |
| attr_accessor :tweets | |
| def each | |
| tweets.each { |tweet| yield tweet } | |
| end | |
| end | |
|  | |
| class Timeline | |
| attr_accessor :tweets | |
| def each(&block) # turn block to proc object | |
| tweets.each(&block) # turn proc object to block | |
| end | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment