DISQUS

drawohara: [Rails] limiting tmp/sessions to a maximum number of sessions files

  • s · 1 year ago
    is there a particular reason you used lambda instead of a method call?
  • drawohara · 1 year ago
  • s · 1 year ago
    himm, is that some sort of metaphorical answer i didn't get? =)
  • drawohara · 1 year ago
    funny - the comment got eaten in the email response i sent (disqus let's you reply via email if you're logged in, etc) anyhow, this is what i'd said:


    yeah there are a few reasons

    1) if i used functions i'd have to pass info to each of them. when i'm working up a short script it's sometime easier to make functions work 'over' data (closure style) since it allows me to play around with what the lambda's do without concern for data passing or signature design. note how the lambdas affect the var 'n' as an example.

    2) def is ugly for short scripts.

    as a script like this matured i'd steer towards a class based approach since it allows the same functionality in a more 'normal' rubylike way of doing things: namely encapsulating the data functions work over as instance vars. nevertheless if you imagine that script being wrapped in 'class Wrapper...' and the data the lambdas work on as @ivars you'll see that using closures in this way effectively makes the script an 'instance' of an object whose class is defined by the script itself.

    for example:

    * encapsulation via objects


    class Example
    def initialize
    @x = 40
    end

    def inc!
    @x += 2
    end
    end

    example = Example.new

    example.inc!


    * encapsulation via closures



    #! /bin/bash

    x = 40

    inc = lambda{ x += 2 }

    inc.call