[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Question about magic enumerators in > 1.8

dblack

7/9/2006 2:03:00 PM

6 Answers

James Gray

7/9/2006 2:33:00 PM

0

On Jul 9, 2006, at 9:03 AM, dblack@wobblini.net wrote:

> Hi --
>
> Can someone jog my memory, and/or enlighten me, as to what a case
> would be where it's useful to have:
>
> array.map
>
> return an Enumerator? I can't seem to come up with an example where:
>
> array.map.anything
>
> is different from
>
> array.anything
>
> Thanks --

Well, you can use it to roll the oft-requested map_with_index():

array.each_with_index.map { |obj, i| .. }

That's not a map() example though, so maybe not what you wanted.

James Edward Gray II

dblack

7/9/2006 2:36:00 PM

0

Logan Capaldo

7/10/2006 2:08:00 AM

0


On Jul 9, 2006, at 10:36 AM, dblack@wobblini.net wrote:

> Hi --
>
> On Sun, 9 Jul 2006, James Edward Gray II wrote:
>
>> On Jul 9, 2006, at 9:03 AM, dblack@wobblini.net wrote:
>>
>>> Hi --
>>> Can someone jog my memory, and/or enlighten me, as to what a case
>>> would be where it's useful to have:
>>>
>>> array.map
>>> return an Enumerator? I can't seem to come up with an example
>>> where:
>>>
>>> array.map.anything
>>> is different from
>>>
>>> array.anything
>>> Thanks --
>>
>> Well, you can use it to roll the oft-requested map_with_index():
>>
>> array.each_with_index.map { |obj, i| .. }
>>
>> That's not a map() example though, so maybe not what you wanted.
>
> Right, it isn't :-) I'm still looking for a map example.
>
>
> David
>
> --
> http://www.rubypoweran... => Ruby/Rails training & consultancy
> http://www.manning... => RUBY FOR RAILS, the Ruby book for
> Rails developers
> http://dablog.r... => D[avid ]A[. ]B[lack's][ Web]log
> dblack@wobblini.net => me
>

Contrived example: I know a method I'm about to call iterates thru me
with each doing something. It's the last thing it does and what the
results of the thing it does rather than myself at the end:

some_method(array.map)

Ok so it's a REALLY contrived example.


ts

7/10/2006 9:23:00 AM

0

>>>>> "d" == dblack <dblack@wobblini.net> writes:

d> return an Enumerator? I can't seem to come up with an example where:

d> array.map.anything

d> is different from

d> array.anything

moulon% ./ruby -e '[12].map.with_index {}'
moulon%

moulon% ./ruby -e '[12].with_index {}'
-e:1: undefined method `with_index' for [12]:Array (NoMethodError)
moulon%


p.s.:
:-))))


--

Guy Decoux

Christian Neukirchen

7/10/2006 11:06:00 AM

0

dblack@wobblini.net writes:

> Hi --
>
> Can someone jog my memory, and/or enlighten me, as to what a case
> would be where it's useful to have:
>
> array.map
>
> return an Enumerator? I can't seem to come up with an example where:
>
> array.map.anything
>
> is different from
>
> array.anything

names.map { |x| x.size }.max, and you don't need an immediate array.

> Thanks --
>
>
> David

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...

dblack

7/10/2006 4:51:00 PM

0