Professional Web Applications Themes

Enumerable#find - Ruby

Would it make sense to allow #find to take an argument or a block (but not both)? I'd like this x.find {|a| a==y } to be shortened to x.find(y) The only question this raises in my mind is: Do we assume == or === or what? I would prefer/expect ==. Synonyms and related methods (#detect, #find_all, ...) would be similarly affected. Comments?? Hal...

  1. #1

    Default Enumerable#find

    Would it make sense to allow #find to take an argument or a block
    (but not both)?

    I'd like this

    x.find {|a| a==y }

    to be shortened to

    x.find(y)

    The only question this raises in my mind is: Do we assume == or === or
    what? I would prefer/expect ==.

    Synonyms and related methods (#detect, #find_all, ...) would be
    similarly affected.

    Comments??


    Hal



    Hal Guest

  2. #2

    Default Re: Enumerable#find

    Hi,

    At Fri, 16 Jan 2004 22:29:04 +0900,
    Hal Fulton wrote: 

    It conflicts current spec, which takes the value when nothing
    found. You would need another name.

    --
    Nobu Nakada


    nobu.nokada@softhome.net Guest

  3. #3

    Default Re: Enumerable#find

    net wrote: 
    >
    >
    > It conflicts current spec, which takes the value when nothing
    > found. You would need another name.[/ref]

    I don't understand. Do you mean this has changed in 1.8?

    find traditionally has returned nil when it fails.

    Can you give me an example?


    Thanks much,

    Hal



    Hal Guest

  4. #4

    Default Re: Enumerable#find

    Hi,

    At Fri, 16 Jan 2004 22:50:38 +0900,
    Hal Fulton wrote: 
    > >
    > >
    > > It conflicts current spec, which takes the value when nothing
    > > found. You would need another name.[/ref]
    >
    > I don't understand. Do you mean this has changed in 1.8?
    >
    > find traditionally has returned nil when it fails.[/ref]

    Sorry, it was wrong a little. Enumerable#find calls the
    optional parameter `ifnone' and returns its result, when no
    object matches. In 1.6 or former, nil returned regardless of
    the result.
     

    $ ruby -e 'p [1,2,3].find(proc{puts "not found"; true}){|x|x.zero?}'
    not found
    true

    --
    Nobu Nakada


    nobu.nokada@softhome.net Guest

  5. #5

    Default Re: Enumerable#find


    <net> schrieb im Newsbeitrag
    news:nakada.kanuma.tochigi.jp... 
    > >
    > > I don't understand. Do you mean this has changed in 1.8?
    > >
    > > find traditionally has returned nil when it fails.[/ref]
    >
    > Sorry, it was wrong a little. Enumerable#find calls the
    > optional parameter `ifnone' and returns its result, when no
    > object matches. In 1.6 or former, nil returned regardless of
    > the result.

    >
    > $ ruby -e 'p [1,2,3].find(proc{puts "not found"; true}){|x|x.zero?}'
    > not found
    > true[/ref]

    Apart from that, many cases of arr.find {|x| x==i} can be dealt with by
    arr.include?( i ) - at least those where one does not need the element
    returned but just the information whether it's there or not. But I guess,
    you asked because of the other cases... :-)

    Regards

    robert

    Robert Guest

Similar Threads

  1. collect! and Enumerable
    By Jim in forum Ruby
    Replies: 5
    Last Post: January 14th, 10:16 PM
  2. An addition to Array (or Enumerable)?
    By Harry in forum Ruby
    Replies: 11
    Last Post: December 29th, 02:05 PM
  3. Enumerable#inject is surprising me...
    By Nathaniel Talbott in forum Ruby
    Replies: 15
    Last Post: October 20th, 11:53 AM
  4. Why isn't Enumerable in StringIO?
    By Jason Creighton in forum Ruby
    Replies: 1
    Last Post: July 26th, 05:14 AM
  5. A different Version of Enumerable#inject
    By Mauricio Fernández in forum Ruby
    Replies: 6
    Last Post: July 25th, 02:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139