Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Lambdas and Pops - Jan Hettich

Lambdas and Pops - Jan Hettich

Las Vegas Ruby Group

June 18, 2014


  1. λ's and POP's
    Jan Hettich

    View full-size slide

  2. Two kinds of Proc
    “plain old proc” lambda
    closure? yes yes
    class Proc Proc
    .lambda? false true
    semantics yield invocation
    similar to block method

    View full-size slide

  3. Creation

    “Plain old procs”
    ● Kernel.proc {|args| code}
    ● Proc.new {|args| code}

    ● Kernel.lambda {|args| code}
    ● literal: ->(args) {code}
    ● Method.to_proc

    View full-size slide

  4. call syntax

    direct call
    – foo.call(args)
    – foo[args]
    – foo.(args)

    block-style call
    – iterator args, &foo

    View full-size slide

  5. call semantics

    yield semantics (blocks, pops)
    ● similar to parallel assignment
    ● return => lexically enclosing method

    invocation semantics (methods, lambdas)
    ● number of arguments must match
    ● “diminutive return”

    View full-size slide

  6. Let's look at some code

    A Digression on Method

    Argument Passing

    return statement

    Using next

    New behavior in Ruby 2.1.0

    View full-size slide