Chaos Project

RPG Maker => RPG Maker Scripts => Topic started by: fugibo on November 15, 2009, 06:22:21 pm

Title: Uses of Enumerable#collect?
Post by: fugibo on November 15, 2009, 06:22:21 pm
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.
Title: Re: Uses of Enumerable#collect?
Post by: Blizzard on November 16, 2009, 04:08:16 am
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.
Title: Re: Uses of Enumerable#collect?
Post by: Zeriab on November 17, 2009, 05:01:50 pm
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
Title: Re: Uses of Enumerable#collect?
Post by: fugibo on November 18, 2009, 06:46:50 pm
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.
Title: Re: Uses of Enumerable#collect?
Post by: Zeriab on November 19, 2009, 05:18:39 pm
No wonder you couldn't understand its usefulness XD