Default argument values for blocks

Discussion in 'Ruby' started by Daniel Schierbeck, Oct 11, 2005.

  1. Is there a reason why I can't do this?

    foo = lambda { |foo = bar| puts foo }
    foo.call

    I can't think of any good reason why this isn't valid.


    Cheers,
    Daniel Schierbeck
     
    Daniel Schierbeck, Oct 11, 2005
    #1
    1. Advertising

  2. Daniel Schierbeck wrote:
    > Is there a reason why I can't do this?
    >
    > foo = lambda { |foo = bar| puts foo }
    > foo.call
    >
    > I can't think of any good reason why this isn't valid.
    >
    >
    > Cheers,
    > Daniel Schierbeck


    I of course meant |foo = "bar"|

    Sorry
     
    Daniel Schierbeck, Oct 11, 2005
    #2
    1. Advertising

  3. Hi,

    In message "Re: Default argument values for blocks"
    on Wed, 12 Oct 2005 01:56:52 +0900, Daniel Schierbeck <> writes:

    |Is there a reason why I can't do this?
    |
    | foo = lambda { |foo = bar| puts foo }
    | foo.call
    |
    |I can't think of any good reason why this isn't valid.

    Mostly because yacc does not allow it. It confuses

    lambda { |foo = bar| puts foo }

    as

    lambda { |foo = (bar| puts foo) }

    and causes syntax error.

    matz.
     
    Yukihiro Matsumoto, Oct 11, 2005
    #3
  4. Hi,

    In message "Re: Default argument values for blocks"
    on Wed, 12 Oct 2005 02:25:33 +0900, Yukihiro Matsumoto <> writes:

    ||Is there a reason why I can't do this?
    ||
    || foo = lambda { |foo = bar| puts foo }
    || foo.call
    ||
    ||I can't think of any good reason why this isn't valid.
    |
    |Mostly because yacc does not allow it. It confuses
    |
    | lambda { |foo = bar| puts foo }
    |
    |as
    |
    | lambda { |foo = (bar| puts foo) }
    |
    |and causes syntax error.

    For your information, you can do

    foo = ->(foo="bar"){puts foo}
    foo.call

    in 1.9.

    matz.
     
    Yukihiro Matsumoto, Oct 11, 2005
    #4
  5. Daniel Schierbeck

    Ara.T.Howard Guest

    On Wed, 12 Oct 2005, Yukihiro Matsumoto wrote:

    > Hi,
    >
    > In message "Re: Default argument values for blocks"
    > on Wed, 12 Oct 2005 02:25:33 +0900, Yukihiro Matsumoto <> writes:
    >
    > ||Is there a reason why I can't do this?
    > ||
    > || foo = lambda { |foo = bar| puts foo }
    > || foo.call
    > ||
    > ||I can't think of any good reason why this isn't valid.
    > |
    > |Mostly because yacc does not allow it. It confuses
    > |
    > | lambda { |foo = bar| puts foo }
    > |
    > |as
    > |
    > | lambda { |foo = (bar| puts foo) }
    > |
    > |and causes syntax error.
    >
    > For your information, you can do
    >
    > foo = ->(foo="bar"){puts foo}
    > foo.call
    >
    > in 1.9.


    -> means lambda?

    -a
    --
    ===============================================================================
    | email :: ara [dot] t [dot] howard [at] noaa [dot] gov
    | phone :: 303.497.6469
    | Your life dwells amoung the causes of death
    | Like a lamp standing in a strong breeze. --Nagarjuna
    ===============================================================================
     
    Ara.T.Howard, Oct 11, 2005
    #5
  6. Yukihiro Matsumoto wrote:
    > Hi,
    >
    > In message "Re: Default argument values for blocks"
    > on Wed, 12 Oct 2005 02:25:33 +0900, Yukihiro Matsumoto <> writes:
    >
    > ||Is there a reason why I can't do this?
    > ||
    > || foo = lambda { |foo = bar| puts foo }
    > || foo.call
    > ||
    > ||I can't think of any good reason why this isn't valid.
    > |
    > |Mostly because yacc does not allow it. It confuses
    > |
    > | lambda { |foo = bar| puts foo }
    > |
    > |as
    > |
    > | lambda { |foo = (bar| puts foo) }
    > |
    > |and causes syntax error.
    >
    > For your information, you can do
    >
    > foo = ->(foo="bar"){puts foo}
    > foo.call
    >
    > in 1.9.
    >
    > matz.
    >
    >


    Cool. Is "->" the same as "lambda"? Can you write this:

    collection.each -> (element) { puts element }
     
    Daniel Schierbeck, Oct 11, 2005
    #6
  7. Yukihiro Matsumoto wrote:
    > Mostly because yacc does not allow it. It confuses
    >
    > lambda { |foo = bar| puts foo }
    >
    > as
    >
    > lambda { |foo = (bar| puts foo) }
    >
    > and causes syntax error.


    yacc? Maybe I was informed wrong, but I was under the impression that
    Ruby used a hand-rolled parser.
     
    Kevin Ballard, Oct 11, 2005
    #7
  8. Yukihiro Matsumoto wrote:
    > Hi,
    >
    > In message "Re: Default argument values for blocks"
    > on Wed, 12 Oct 2005 02:25:33 +0900, Yukihiro Matsumoto <> writes:
    >
    > ||Is there a reason why I can't do this?
    > ||
    > || foo = lambda { |foo = bar| puts foo }
    > || foo.call
    > ||
    > ||I can't think of any good reason why this isn't valid.
    > |
    > |Mostly because yacc does not allow it. It confuses
    > |
    > | lambda { |foo = bar| puts foo }
    > |
    > |as
    > |
    > | lambda { |foo = (bar| puts foo) }
    > |
    > |and causes syntax error.
    >
    > For your information, you can do
    >
    > foo = ->(foo="bar"){puts foo}
    > foo.call
    >
    > in 1.9.
    >
    > matz.


    MY EYES!!!!

    Please, no.

    Dan
     
    Daniel Berger, Oct 11, 2005
    #8
  9. Daniel Schierbeck

    Eric Hodel Guest

    On Oct 11, 2005, at 12:21 PM, Kevin Ballard wrote:

    > Yukihiro Matsumoto wrote:
    >> Mostly because yacc does not allow it.

    >
    > yacc? Maybe I was informed wrong, but I was under the impression that
    > Ruby used a hand-rolled parser.


    You were informed wrong.
     
    Eric Hodel, Oct 11, 2005
    #9
  10. Hi --

    On Wed, 12 Oct 2005, Yukihiro Matsumoto wrote:

    > Hi,
    >
    > In message "Re: Default argument values for blocks"
    > on Wed, 12 Oct 2005 02:25:33 +0900, Yukihiro Matsumoto <> writes:
    >
    > ||Is there a reason why I can't do this?
    > ||
    > || foo = lambda { |foo = bar| puts foo }
    > || foo.call
    > ||
    > ||I can't think of any good reason why this isn't valid.
    > |
    > |Mostly because yacc does not allow it. It confuses
    > |
    > | lambda { |foo = bar| puts foo }
    > |
    > |as
    > |
    > | lambda { |foo = (bar| puts foo) }
    > |
    > |and causes syntax error.
    >
    > For your information, you can do
    >
    > foo = ->(foo="bar"){puts foo}
    > foo.call
    >
    > in 1.9.


    Is this definitely going to remain? (Please say no.... :)


    David

    --
    David A. Black
     
    David A. Black, Oct 11, 2005
    #10
  11. Hi,

    In message "Re: Default argument values for blocks"
    on Wed, 12 Oct 2005 02:40:48 +0900, "Ara.T.Howard" <> writes:

    |> foo = ->(foo="bar"){puts foo}

    |-> means lambda?

    Yes, as in Perl6. "lambda" here means lambda in other languages, not
    the lambda method in Ruby.

    matz.
     
    Yukihiro Matsumoto, Oct 12, 2005
    #11
  12. Hi,

    In message "Re: Default argument values for blocks"
    on Wed, 12 Oct 2005 06:40:07 +0900, "David A. Black" <> writes:

    |> foo = ->(foo="bar"){puts foo}

    |Is this definitely going to remain? (Please say no.... :)

    It is, but appearance may be changed. But I haven't got the better
    one.

    matz.
     
    Yukihiro Matsumoto, Oct 12, 2005
    #12
  13. Daniel Schierbeck

    Eric Mahurin Guest

    --- Yukihiro Matsumoto <> wrote:

    > Hi,
    >=20
    > In message "Re: Default argument values for blocks"
    > on Wed, 12 Oct 2005 02:40:48 +0900, "Ara.T.Howard"
    > <> writes:
    >=20
    > |> foo =3D ->(foo=3D"bar"){puts foo}
    >=20
    > |-> means lambda?
    >=20
    > Yes, as in Perl6. "lambda" here means lambda in other
    > languages, not
    > the lambda method in Ruby.
    >=20
    > matz.


    Other than syntax, what's the difference between this and the
    Proc that comes from the lambda method? Maybe how return,
    break, etc is handled?

    In addition to handling defaults in arguments, I think these
    issues need to be addressed in blocks/lambdas:

    - all variables in the defining scope of a block/lambda are
    prevented from being GCed by the block/lambda (because of the
    #binding). This can easily result in a leaky program if not
    careful. I discussed several solutions to this in a previous
    thread.

    - a facility for making variables local (regardless of whether
    a variable of the same name exists in the defining scope).=20
    This would also be good to have for a begin-end type block (I'd
    like to have it for making inlined macros where I don't want to
    worry about what variables are already used). I discussed
    several solutions for this in another thread.

    Also, I saw some slides for ruby 2 back in 2003 showing that
    arguments will always be local to the block and other variables
    in the block will be in the same scope as the defining scope.=20
    I think always making arguments local is a good thing, but
    never localizing the other variables will be problematic if
    another local variable facility is not provided.




    =09
    =09
    __________________________________=20
    Yahoo! Mail - PC Magazine Editors' Choice 2005=20
    http://mail.yahoo.com
     
    Eric Mahurin, Oct 12, 2005
    #13
  14. Hi --

    On Wed, 12 Oct 2005, Yukihiro Matsumoto wrote:

    > Hi,
    >
    > In message "Re: Default argument values for blocks"
    > on Wed, 12 Oct 2005 06:40:07 +0900, "David A. Black" <> writes:
    >
    > |> foo = ->(foo="bar"){puts foo}
    >
    > |Is this definitely going to remain? (Please say no.... :)
    >
    > It is, but appearance may be changed. But I haven't got the better
    > one.


    I definitely think a keyword would be better than -> .


    David

    --
    David A. Black
     
    David A. Black, Oct 12, 2005
    #14
  15. Hi,

    In message "Re: Default argument values for blocks"
    on Wed, 12 Oct 2005 10:01:44 +0900, Eric Mahurin <> writes:

    |Other than syntax, what's the difference between this and the
    |Proc that comes from the lambda method? Maybe how return,
    |break, etc is handled?

    They are same, same class, same behavior, except for full argument
    syntax of method arguments.

    |In addition to handling defaults in arguments, I think these
    |issues need to be addressed in blocks/lambdas:
    |
    |- all variables in the defining scope of a block/lambda are
    |prevented from being GCed by the block/lambda (because of the
    |#binding). This can easily result in a leaky program if not
    |careful. I discussed several solutions to this in a previous
    |thread.

    I think this issue should _not_ be solved by making new lambda-like
    object, but something like making eval a keyword, so that non
    referenced local variable can be erased unless eval is called within
    the scope.

    |- a facility for making variables local (regardless of whether
    |a variable of the same name exists in the defining scope).
    |This would also be good to have for a begin-end type block (I'd
    |like to have it for making inlined macros where I don't want to
    |worry about what variables are already used). I discussed
    |several solutions for this in another thread.

    In 1.9, you can declare block local variables explicitly, by the
    syntax:

    foo {|a; b, c| ...} # b and c are block local

    so that

    b=1
    5.times{|i;b|
    b=5 # modifying in-block "b"
    p [i,b]
    }
    p b # outer "b" remain unchanged

    |Also, I saw some slides for ruby 2 back in 2003 showing that
    |arguments will always be local to the block and other variables
    |in the block will be in the same scope as the defining scope.
    |I think always making arguments local is a good thing, but
    |never localizing the other variables will be problematic if
    |another local variable facility is not provided.

    See above.

    matz.
     
    Yukihiro Matsumoto, Oct 12, 2005
    #15
  16. Hi,

    In message "Re: Default argument values for blocks"
    on Wed, 12 Oct 2005 10:13:31 +0900, "David A. Black" <> writes:

    |I definitely think a keyword would be better than -> .

    Maybe. Some prefer verbosity to clarity. So what keyword you think
    "the best"?

    matz.
     
    Yukihiro Matsumoto, Oct 12, 2005
    #16
  17. Daniel Schierbeck

    ES Guest

    Yukihiro Matsumoto wrote:
    > Hi,
    >
    > In message "Re: Default argument values for blocks"
    > on Wed, 12 Oct 2005 06:40:07 +0900, "David A. Black" <> writes:
    >
    > |> foo = ->(foo="bar"){puts foo}
    >
    > |Is this definitely going to remain? (Please say no.... :)
    >
    > It is, but appearance may be changed. But I haven't got the better
    > one.


    What about either

    block = lambda(foo = 'bar') { puts foo } # Implicit

    or

    block = lambda(foo = 'bar') {|foo| puts foo } # Explicit

    Or is -> solving an entirely different problem? (If so, would
    you mind stating the problem again or pointing me to an earlier
    discussion?)

    > matz.


    E
     
    ES, Oct 12, 2005
    #17
  18. Daniel Schierbeck

    Ara.T.Howard Guest

    On Wed, 12 Oct 2005, Yukihiro Matsumoto wrote:

    > Hi,
    >
    > In message "Re: Default argument values for blocks"
    > on Wed, 12 Oct 2005 02:40:48 +0900, "Ara.T.Howard" <> writes:
    >
    > |> foo = ->(foo="bar"){puts foo}
    >
    > |-> means lambda?
    >
    > Yes, as in Perl6. "lambda" here means lambda in other languages, not
    > the lambda method in Ruby.


    by that you mean it is a 'real' method - not a proc object?

    i like -> btw. though =>> has a bit more 'weight'.

    regards.

    -a
    --
    ===============================================================================
    | email :: ara [dot] t [dot] howard [at] noaa [dot] gov
    | phone :: 303.497.6469
    | Your life dwells amoung the causes of death
    | Like a lamp standing in a strong breeze. --Nagarjuna
    ===============================================================================
     
    Ara.T.Howard, Oct 12, 2005
    #18
  19. Hi,

    In message "Re: Default argument values for blocks"
    on Wed, 12 Oct 2005 10:39:39 +0900, "Ara.T.Howard" <> writes:

    |> |-> means lambda?
    |>
    |> Yes, as in Perl6. "lambda" here means lambda in other languages, not
    |> the lambda method in Ruby.
    |
    |by that you mean it is a 'real' method - not a proc object?

    By that I mean, it is a syntax, which gives you a Proc object.

    |i like -> btw. though =>> has a bit more 'weight'.

    It depends on your taste.

    matz.
     
    Yukihiro Matsumoto, Oct 12, 2005
    #19
  20. Hi,

    In message "Re: Default argument values for blocks"
    on Wed, 12 Oct 2005 03:16:52 +0900, Daniel Schierbeck <> writes:

    |Cool. Is "->" the same as "lambda"? Can you write this:
    |
    | collection.each -> (element) { puts element }

    Yes. It is still experimental though.

    matz.
     
    Yukihiro Matsumoto, Oct 12, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arjen
    Replies:
    3
    Views:
    446
    Scott Allen
    Feb 27, 2005
  2. Noah
    Replies:
    2
    Views:
    270
  3. Replies:
    3
    Views:
    270
    Tim Chase
    Jun 1, 2006
  4. matt
    Replies:
    1
    Views:
    270
    George Ogata
    Aug 6, 2004
  5. Steven Taylor
    Replies:
    9
    Views:
    256
    Brian Candler
    Apr 27, 2009
Loading...

Share This Page