From: Mauricio Fernandez Date: 2006-01-08T03:27:07+09:00 Subject: Re: Iterator Fu Failing Me On Sun, Jan 08, 2006 at 01:13:00AM +0900, Robert Klemme wrote: > Bob Hutchison wrote: > >On Jan 6, 2006, at 11:36 PM, James Edward Gray II wrote: > > > >>That works. Now can anyone give me a version of the middle section > >>that doesn't require I call parse?() 50 times? I want something > >>close to: > >> > >> elements = tokens.map do |token| > >> [ClassA, ClassB, ClassC].find { |kind| kind.parse?(token) } > >> end > >> > >>Except that I want the return result of parse?(), instead of the > >>class that took it. > > > >Why not: > > > >elements = tokens.map do |token| > > result = nil > > [ClassA, ClassB, ClassC].find { |kind| result = kind.parse?(token) } > >end > > Because this won't return the proper value from the block. You need at > least to add a line with "result" after the #find. :-) And then it's much > more inelegant than using #detect. :-) Actually, enum.c: rb_define_method(rb_mEnumerable,"find", enum_find, -1); rb_define_method(rb_mEnumerable,"detect", enum_find, -1); ... (1..10).detect{|x| 10*x > 50} # => 6 ... -- Mauricio Fernandez