block returns and hash element returns

Discussion in 'Ruby' started by Trans, Nov 6, 2005.

  1. Trans

    Trans Guest

    A question and a thought (or two).

    Is there a way to return from a block without returning from the whole
    method? Eg.

    # This behavior
    def double_collect( array )
    array.collect { |v| v*2 }
    end

    # Nope, returns from double_collect
    def double_collect( array )
    array.collect { |v| return v*2 }
    end

    # Nope, returns only first iteration
    def double_collect( array )
    array.collect { |v| break v*2 }
    end

    Would something like #local_return be useful?

    The reason I ask is b/c I was also thinking about a possbile special
    form of 'return' that can handle hash assocations:

    def double_collect( array )
    array.collect { |v| local_return v => v*2 }
    end
    double_collect( [1,2,3] ) #=> { 1=>2, 2=>4, 3=>6 }

    Presently I use a facet called #graph to do this:

    array.graph { |v| [v, v*2] }

    But returning that array just feels wrong (not to mention inefficient).

    Thanks,
    T.
     
    Trans, Nov 6, 2005
    #1
    1. Advertisements

  2. Trans

    Guest

    On Nov 6, 2005, at 1:07 AM, Trans wrote:
    > A question and a thought (or two).
    >
    > Is there a way to return from a block without returning from the whole
    > method?


    I think you are looking for 'next'

    irb(main):001:0> [1,2,3,4].each { |x| next if x%2==0; puts "#{x} is
    odd" }
    1 is odd
    3 is odd
    => [1, 2, 3, 4]



    Gary Wright
     
    , Nov 6, 2005
    #2
    1. Advertisements

  3. Trans

    Trans Guest

    #next, of course! Thank you.

    Okay, so use #next in place of #local_return.

    h1 = { :a => 1, :b => 2, :c => 3 }
    h2 = h1.collect { |k,v| next k => v*2 }
    h2 => { :a => 2, :b => 4, :c => 6 }

    Could do this by modifying collect to merge hashes if they are the
    yield result, but that's not exactly the same. since it would make it
    impossible to collect hashes, and it would have not advantage over the
    #graph method either --just substituting a hash for the array.

    T.
     
    Trans, Nov 6, 2005
    #3
    1. Advertisements

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. Woolly Mittens
    Replies:
    8
    Views:
    624
    Woolly Mittens
    Sep 22, 2003
  2. tankbattle
    Replies:
    1
    Views:
    1,088
    George Bina
    Jul 21, 2006
  3. morrell
    Replies:
    1
    Views:
    1,210
    roy axenov
    Oct 10, 2006
  4. HANM
    Replies:
    2
    Views:
    1,512
    Joseph Kesselman
    Jan 29, 2008
  5. rp
    Replies:
    1
    Views:
    854
    red floyd
    Nov 10, 2011
  6. Srijayanth Sridhar
    Replies:
    19
    Views:
    991
    David A. Black
    Jul 2, 2008
  7. Phlip
    Replies:
    9
    Views:
    263
    Phlip
    Mar 9, 2009
  8. Guest
    Replies:
    1
    Views:
    152
    Thomas 'PointedEars' Lahn
    Oct 9, 2007
Loading...