Permalink
Browse files

Procs

  • Loading branch information...
1 parent d70d0df commit f9cf5948671c304506dcda1b7be3e8ade03a1a32 @weppos committed Feb 25, 2011
Showing with 225 additions and 3 deletions.
  1. +2 −0 procs/00.md
  2. +131 −1 procs/01_blocks.md
  3. +48 −0 procs/02_procs.md
  4. +44 −2 procs/03_dsl.md
View
@@ -0,0 +1,2 @@
+!SLIDE subsection
+# Block / Lambda / Procs
View
@@ -1,3 +1,133 @@
!SLIDE
-# Block / Lambda / Procs
+# Blocks
+
+ @@@ Ruby
+ a = [:foo, :bar, :baz]
+
+ a.each { |value| puts "Value: #{value}" }
+ # Value: foo
+ # Value: bar
+ # Value: baz
+
+ a.each do |value|
+ puts "Value: #{value}"
+ end
+ # Value: foo
+ # Value: bar
+ # Value: baz
+
+
+!SLIDE
+# Implicit vs Explicit Block
+
+ @@@ Ruby
+ def benchmark(&block)
+ stime = Time.now
+ yield
+ etime = Time.now
+ puts "#{etime - stime} seconds"
+ end
+
+ benchmark do
+ puts "Bunch of code"
+ sleep(1)
+ end
+
+
+!SLIDE
+# Implicit vs Explicit Block
+
+ @@@ Ruby
+ def benchmark
+ stime = Time.now
+ yield
+ etime = Time.now
+ puts "#{etime - stime} seconds"
+ end
+
+ def benchmark(&block)
+ stime = Time.now
+ block.call
+ etime = Time.now
+ puts "#{etime - stime} seconds"
+ end
+
+
+!SLIDE
+# Block with parameters
+
+ @@@ Ruby
+ class CustomCache < Struct.new(:buffer)
+ def cache
+ yield @buffer
+ @buffer
+ end
+ end
+
+ class CustomCache < Struct.new(:buffer)
+ def cache(&block)
+ block.call(@buffer)
+ @buffer
+ end
+ end
+
+ CustomCache.new.cache do |c|
+ # ...
+ c << "foo"
+ # ...
+ c << "bar"
+ end
+ # => ["foo", "bar"]
+
+
+!SLIDE
+# Initialization block
+
+ @@@ Ruby
+ class Article < Struct.new(:title, published_at)
+ def initialize(*args)
+ super
+ yield self if block_given? # <- Pass self as parameter
+ self
+ end
+ end
+
+ Article.new do |article|
+ article.title = "Foo"
+ article.published_at = Today
+ end
+ # => #<Article >
+
+
+!SLIDE center
+# yield vs &block
+
+## yield is more efficient
+## use &block when you need treat the block as variable
+
+
+!SLIDE
+# yield vs &block
+
+ @@@ Ruby
+ class ColorPicker
+ def initialize
+ @colors = []
+ end
+ def <<(*colors)
+ @colors.push(*colors)
+ end
+ def each(&block) # <-- passing &block to Array#each
+ @colors.each(&block)
+ end
+ end
+
+ c = ColorPicker.new
+ c << "yellow", "red"
+
+ c.each do |color|
+ puts color
+ end
+ # => yellow
+ # => red
View
@@ -0,0 +1,48 @@
+!SLIDE
+# Proc and Lambda
+
+ @@@ Ruby
+ var = Proc.new { p Time.now }
+ var = proc { p Time.now }
+ var = lambda { p Time.now }
+
+ var.call
+ # => 2011-02-25 15:34:18 +0100
+
+!SLIDE
+# Blocks "are Procs"
+
+ @@@ Ruby
+ def m1(&block)
+ p block.class
+ end
+
+ m1 do
+ ...
+ end
+ # Proc
+
+
+!SLIDE
+# You can pass Proc and Lambda
+
+ @@@ Ruby
+ printer = lambda { |item| p item.upcase }
+ # => #<Proc:0x000001009e8738@(irb):15 (lambda)>
+
+ %w( foo bar baz ).each(&printer)
+ # "FOO"
+ # "BAR"
+ # "BAZ"
+
+
+!SLIDE
+# Symbol#to_proc
+
+ @@@ Ruby
+ %w( foo bar baz ).map { |i| i.upcase }
+ # => ["FOO", "BAR", "BAZ"]
+
+ %w( foo bar baz ).map(&:upcase)
+ # => ["FOO", "BAR", "BAZ"]
+
View
@@ -1,2 +1,44 @@
-!SLIDE
-# DSL
+!SLIDE center
+# Domain Specific Language (DSL)
+
+
+!SLIDE center
+# Ruby and DSL
+
+## Blocks allow you to declare
+## instructions in custom languages
+## and implement them in Ruby
+
+
+!SLIDE center
+# Example from Capistrano
+
+ @@@ Ruby
+ namespace :deploy do
+ desc <<-DESC
+ Restarts your application.
+ DESC
+ task :restart, :roles => :app, :except => { :no_release => true } do
+ run "touch #{current_path}/tmp/restart.txt"
+ end
+
+
+!SLIDE center
+# Example from Whois
+
+ @@@ Ruby
+ describe Whois::Client do
+ describe "#initialize" do
+ it "does not accept zero parameters" do
+ expect do
+ klass.new
+ end.to raise_error(ArgumentError)
+ end
+
+ it "accepts a settings parameter" do
+ expect do
+ klass.new({ :foo => "bar" })
+ end.to_not raise_error
+ end
+ end
+ end

0 comments on commit f9cf594

Please sign in to comment.