Uses of Enumerable#collect?

Started by fugibo, November 15, 2009, 06:22:21 pm

Previous topic - Next topic

fugibo

Does anyone know any practical uses for Enumerable#collect? The uses of methods such as select, any?, all?, detect, &c. are all pretty obvious (if esoteric), but I can't see any case in which a coder would need an array of values.

Blizzard

It's usually just for clean coding. You can just use numeric constants in RGSS, that should give about the same effect and pretty much suffice. Other than that I don't know what exactly it could be used for other than, well, enumeration.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Zeriab

I prefer it's synonym map simply because it conveys what it does much better imo.
For each key map to a value. Return an array of the values. Let's look at a simple example where you have an array of indexes:
indexes = [1,5,4,12]
actors = indexes.map {|i| $game_actors[i]}


Of course the block can be much more complex so you can come into situations where that can help you simplify matters :3

*hugs*
- Zeriab

fugibo

Ah... I had misunderstood it. I had assumed that it simply returned an array of true or false; it actually returns the value of the block. Millions of times more useful than what I thought it did.

Zeriab

No wonder you couldn't understand its usefulness XD