Ruby Scripting
#2
More About Parent Classes

Tonight I guess I should tell you a bit more about parent classes and why you need them.

Do you really need them?

Happy with a sweat As I told you before, or at least I should have done so back then, that depends on your very personal design. Some people never use them while others do on a daily basis. So you're gonna learn when you should use them or just skip them if there's no use in creating them in the first place.

Code:
class Rect
  def initialize(x, y, w, h)
    @x = x
    @y = y
    @width = w
    @height = h
  end
  attr_accessor :x, :y, :width, :height
end

Do you see that? It's our basic Rect(angle) class! Shocked 

Sarcasm So what?

Really? ? We finally created a working class! That's why it matters a lot! Now tell me if you do feel you need a subclass or parent class of it.

Confused How should I know? You're the one that picked this specific topic!

Right... Sarcasm + Confused If you ever feel like that, probably you won't need either of them. Laughing
Yeah, you could later come up with other classes like other shapes, namely Circle, Triangle, and so on. Honestly you can stick to rectangles only for most of your gaming needs. At the end, you won't deal with any other geometrical shape unless you start using something like OpenGL and start depending on your graphics cards capabilities. Laughing + Tongue sticking out 

So how do you know when to subclass some bunch of code? Here's when you should do that!

Code:
class Person
  def initialize(name)
    @name = name
    @age = 0
    @ssid = "0"
    @job = ""
    @skin_color = Color.new(0,0,0)
    @eye_color = Color.new(0,0,0)
    @hair_color = Color.new(0,0,0)
    @hobbies = []
    @relatives = []
    @phone_number = ""
  end
  attr_accessor :name, :age, :ssid, :job, :skin_color, :eye_color, :hair_color, :phone_number
  attr_reader :hobbies, :relatives
end

Do you know every single person share stuff in common?

I guess so... Indifferent 

Well, that could later become our first parent class!

How so!? Shocked 

That's easy to explain! We know many people share traits like name, age, skin, eyes, hair, and so on. With this we can already create an object to represent a person like a student or coworker or site administrator or just some dreamer that complains about heat and many more!

So what's next? Grinning 

Really? Coming up next would be our first child class! Let's define one right away!

Code:
class Gamer < Person
  def initialize(name)
    super
    @played_games = []
    @favorite_games = []
    @despised_games = []
    @hours_spent_gaming = 0
  end
  attr_reader :played_games, :favorite_games, :despised_games
  attr_accessor :hours_spent_gaming
end

Shocked Wow! You defined a Gamer gamer as inferior, less valuable than an average person!

Yeah, I did! Laughing + Tongue sticking out But don't let that offend you in any way possible. I did that only for scripting purposes. For languages like Ruby anything that's related somehow to another basic class is "inferior" for a different reason. Actually it means it descends from a basic template called Person. Since a person isn't always a gamer, it's better to place Person first and Gamer as is direct subordinate. Nope, it's not his or her employee or servant, it's just a class that will call a Person's methods and add new ones that won't describe other persons but just gamers. I think it might be easy to understand if I show you another example now...

Go ahead! Grinning 

Sarcasm OK, this time we'll talk about lawyers!

Code:
class Lawyer < Person
  def initialize(name)
    super(name)
    @degree = ""
    @got_license = true
    @location = ""
    @workdays = []
    @customers = []
    @work_phone_number = ""
  end
  attr_accessor :degree, :got_license, :work_phone_number
  attr_reader :workdays, :customers
end

As you can see a lawyer, even if nobody likes them except for their own families Laughing also share common traits but got others as well. If we didn't have a Person as parent class, we would keep repeating code over and over again.

Shocked So you're teaching us how to become lazy as scripters!

Sad You make it sound as a bad thing...

Shocked But it is bad indeed!

Sarcasm Nope, it isn't. At least not in this case. Ruby's and any other OOP's goal is to let you reuse code by subclassing stuff as much as needed. This way you won't need to copy thousands of lines of code every single time you wanna add new features or change them. Plus, why on earth would a busy Lawyer care about videogames? He needs to prepare for his next trial after all. He'd probably hate you for thinking all lawyers and even Judge judges are like those found in Ace Attorney games. Laughing

Shocked Hey! I liked those games! They were quite realistic!

O_o? Are they!? Anyway, I've proven that we might need a basic parent class in case we might later define several descendants that will need to call stuff defined in the parent class. This pattern will save us from adding many checks in our code just to define when a person should turn on a TV set or open a briefcase or yell Objection! You wouldn't do that if you were just sitting on a seat like many other passengers while traveling on a bus or train or metro, would you!? Laughing + Tongue sticking out

Now we're gonna discuss what's super!

Batman Batman is super indeed! Shocked Many would say Clark Kent Superman is, but he's just a guy on steroids!

Really? ? Now we got somebody full of fleas that can't waste a chance to show off how he wasted his time reading comics. Laughing 

Actually for Ruby none of them would be super.

Shocked Say what!?

Yeah, none of them are, I do agree with Ruby on this. Laughing + Tongue sticking out The main idea behind it would be that both of them would be living beings so none of them could be called super or need to call super, the Ruby method. Laughing For Ruby super means "Hey! Go call my parent and see if he needs to get some new name!" Laughing Actually Gamer and Lawyer did yell at Ruby they were passing a name to Person. They both did the exact same thing but one of them was lazy as to explicitly pass it and let Person assume he would know what they were handing out to him. Or were they irresponsible enough as to let others blame their parent instead? Confused 

Code:
class Siletrea < WannabeDragonfly
  def initialize(name)
    @name = name
    @claws = []
    @age = 0
    @favorite_habitat = ""
  end
  attr_accessor :name, :age, :favorite_habitat
  attr_reader :claws
end

Wait a second! Shocked Why didn't you define Siletrea as another Person here?

Really? ? Is it my fault she believes she's a dragonfly? Laughing Plus she got claws, Confused and believes she can fly and attack poor people like me. Sad At the end she will fail to achieve her goal because she's a person and so our code will fail as well. Laughing You see, we never defined a WannabeDragonfly class before we defined her. Laughing + Tongue sticking out

By the way, just in case you include arrays or hashes in your classes, you'd usually need getters only. Most of the time you'd just change their contents, not the entire array or hash. Thus there's no need to define something you don't need. That will keep your code clean and won't ask for extra resources that are gonna get wasted anyway. Laughing 

How large can a class ever be?

Err, again, that depends on your project and coding style. Laughing + Tongue sticking out Usually you should only include what's enough to keep it running. Any extras should belong to specialized classes like subclasses alias child classes. There's no specific maximum size, but people say a large script takes longer to load than several short ones and the latter can let you find bugs in no time while the former might make it excessively complex for they might affect many lines of code at a single time. Confused The only way you can be certain if it's a good or bad idea might be to load a project and its backup and see if any of those gets affected by how large the scripts currently are. Still, for a few scripts that won't ever matter. For hundreds of them Confused I can't tell for sure it they will play along as a single, unified team. Happy with a sweat Even so new versions of Ruby might make it a bit harder to notice. Besides, non graphical scripts won't consume as many resources as those manipulating shapes or changing colors on the fly and so on. Laughing 

Warning!

Don't abuse of loops or you'll get stuck in no time! Shocked

Happy Stack Overflow! Laughing + Tongue sticking out  
"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.

[Image: SP1-Scripter.png]
[Image: SP1-Writer.png]
[Image: SP1-Poet.png]
[Image: SP1-PixelArtist.png]
[Image: SP1-Reporter.png]

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! Laughing + Tongue sticking out

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
Reply }


Messages In This Thread
Ruby Scripting - by kyonides - 08-29-2019, 04:51 AM
RE: Ruby Scripting - by kyonides - 08-30-2019, 05:47 AM
RE: Ruby Scripting - by kyonides - 09-03-2019, 07:24 AM
RE: Ruby Scripting - by kyonides - 09-06-2019, 05:46 AM
RE: Ruby Scripting - by kyonides - 09-09-2019, 05:00 AM
RE: Ruby Scripting - by kyonides - 06-05-2021, 08:20 PM
RE: Ruby Scripting - by kyonides - 11-28-2021, 03:31 AM
RE: Ruby Scripting - by kyonides - 05-02-2022, 02:43 AM
RE: Ruby Scripting - by kyonides - 01-27-2023, 08:01 AM
RE: Ruby Scripting - by kyonides - 02-03-2023, 02:42 AM
RE: Ruby Scripting - by DerVVulfman - 02-03-2023, 03:52 AM
RE: Ruby Scripting - by kyonides - 02-03-2023, 04:33 AM
RE: Ruby Scripting - by kyonides - 04-19-2023, 12:47 AM
RE: Ruby Scripting - by kyonides - 04-19-2023, 01:12 AM
RE: Ruby Scripting - by kyonides - 06-08-2023, 09:24 PM
RE: Ruby Scripting - by kyonides - 06-08-2023, 09:46 PM
RE: Ruby Scripting - by kyonides - 06-12-2023, 06:29 AM
RE: Ruby Scripting - by kyonides - 07-14-2023, 10:37 PM
RE: Ruby Scripting - by kyonides - 07-15-2023, 07:32 AM
RE: Ruby Scripting - by kyonides - 07-16-2023, 06:05 AM
RE: Ruby Scripting - by kyonides - 07-17-2023, 04:01 AM
RE: Ruby Scripting - by kyonides - 08-22-2023, 08:59 AM
RE: Ruby Scripting - by kyonides - 08-28-2023, 03:34 AM
RE: Ruby Scripting - by kyonides - 11-18-2023, 09:02 AM
RE: Ruby Scripting - by kyonides - 04-16-2024, 06:57 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
   Ruby - Behind the Scenes DerVVulfman 0 1,148 07-15-2023, 05:52 PM
Last Post: DerVVulfman
Information  Assorted Ruby chm documents. hanetzer 10 35,245 08-17-2020, 04:19 AM
Last Post: kyonides
Brick  Learn Ruby! greenraven 2 7,614 05-16-2014, 12:25 PM
Last Post: greenraven
   Creating Your Own Scripting System DerVVulfman 3 7,467 10-12-2009, 03:37 PM
Last Post: Alpha-Mad
   How to make interesting NPC's without scripting Third333Strike 0 3,846 12-06-2008, 04:59 PM
Last Post: Third333Strike



Users browsing this thread: 4 Guest(s)