Skip to content

Instantly share code, notes, and snippets.

@ilake
Created November 15, 2012 00:06
Show Gist options
  • Select an option

  • Save ilake/4075750 to your computer and use it in GitHub Desktop.

Select an option

Save ilake/4075750 to your computer and use it in GitHub Desktop.
Rubybits2 LEVEL 1: Proc, block, lambda
# & 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