Arrays, Blocks & Obscure Practices
Here's an awful example that shows us what can be done but should NEVER take place. You would only need to use a code like the following one:
Code:
$game_party.actors.map do |actor|
actor.age
end.max
What it does wrong is calling a second method not inside but outside of the block as the next logical step to take once the block finishes executing itself.
Yes, it's still possible to call a method at the end of such a block but it's a bad practice. Some people would write it like this instead:
Code:
$game_party.actors.map { |actor|
actor.age
}.max
It's also obscure for sure. The best idea would be to do something totally different and a lot simpler than any of those 2 very similar options.
Valid Option No. 1
Use 2 lines of code instead
Code:
list = $game_party.actors.map {|actor| actor.age }
list.max
This one is the quickest way to obtain the maximum value in no time and is as valid as the following one.
Valid Option No. 2
Create a specific method and call it from the original one
Code:
def actors_ages
$game_party.actors.map {|actor| actor.age }
end
def find_max_age
actors_ages.max
end
As you can see both options are clean and easily readable. Another advantage is that it will only throw an error if and only if there's an empty actor or the @actors array's original contents have been replaced by improper ones.
By the way, this example is only valid for RMXP because the @actors array contain actual actors. Starting RMVX they simply include Actor ID's so you would need to convert them to actors first and then extract their ages as a second step that's unavoidable.
Use $game_party.members or replace @actors with members.
If you ever paid attention to RMVX ACE default scripts, you might have noticed by now that they almost always rely on the second valid option I exposed above. Well, it's quite useful and can be called from pretty much anywhere without exposing it in an "illegal" way based on OOP principles.
And yes, in Ruby it is quite normal to encapsulate methods and create tons of wrappers for methods belonging to other objects. Game_Party class is a good example of it.
"For God has not destined us for wrath, but for obtaining salvation through our Lord Jesus Christ," 1 Thessalonians 5:9
Maranatha!
The Internet might be either your friend or enemy. It just depends on whether or not she has a bad hair day.
My Original Stories (available in English and Spanish)
List of Compiled Binary Executables I have published...
HiddenChest & Roole
Give me a free copy of your completed game if you include at least 3 of my scripts!
Just some scripts I've already published on the board...
KyoGemBoost XP VX & ACE, RandomEnkounters XP, KSkillShop XP, Kolloseum States XP, KEvents XP, KScenario XP & Gosu, KyoPrizeShop XP Mangostan, Kuests XP, KyoDiscounts XP VX, ACE & MV, KChest XP VX & ACE 2016, KTelePort XP, KSkillMax XP & VX & ACE, Gem Roulette XP VX & VX Ace, KRespawnPoint XP, VX & VX Ace, GiveAway XP VX & ACE, Klearance XP VX & ACE, KUnits XP VX, ACE & Gosu 2017, KLevel XP, KRumors XP & ACE, KMonsterPals XP VX & ACE, KStatsRefill XP VX & ACE, KLotto XP VX & ACE, KItemDesc XP & VX, KPocket XP & VX, OpenChest XP VX & ACE
Maranatha!
The Internet might be either your friend or enemy. It just depends on whether or not she has a bad hair day.
My Original Stories (available in English and Spanish)
List of Compiled Binary Executables I have published...
HiddenChest & Roole
Give me a free copy of your completed game if you include at least 3 of my scripts!
Just some scripts I've already published on the board...
KyoGemBoost XP VX & ACE, RandomEnkounters XP, KSkillShop XP, Kolloseum States XP, KEvents XP, KScenario XP & Gosu, KyoPrizeShop XP Mangostan, Kuests XP, KyoDiscounts XP VX, ACE & MV, KChest XP VX & ACE 2016, KTelePort XP, KSkillMax XP & VX & ACE, Gem Roulette XP VX & VX Ace, KRespawnPoint XP, VX & VX Ace, GiveAway XP VX & ACE, Klearance XP VX & ACE, KUnits XP VX, ACE & Gosu 2017, KLevel XP, KRumors XP & ACE, KMonsterPals XP VX & ACE, KStatsRefill XP VX & ACE, KLotto XP VX & ACE, KItemDesc XP & VX, KPocket XP & VX, OpenChest XP VX & ACE