Object Oriented Ruby

Another intensive week behind me. I spent over 23 hours on coding and solving different challenges or reading Ruby Programming by David Black. I have finally submitted my solutions of a large RSpec challenge. Last exercises took me the most of the time – which was about 12 hours to crack the problem. The joy, in the end, paid off. I was proud of my approach as it wasn’t easy an easy ball to play with.

The last challenge I mean is again purely Test Driven Solution with Rspec file waiting. The below method is adding extra function to the Fixnum class so having “12345.in_words” will give us “twelve thousand three hundred forty five.”  I didn’t think it would be that difficult when I started. I thought about two ways of approaching this problem – either creating string or array and working with that to find a solution or by dividing and modulating numbers. I used recursion and numbers themselves. Here is my code which works for all numbers up to 999 trillions:

screen-shot-2016-10-03-at-21-05-41screen-shot-2016-10-03-at-21-04-02

As I am proud bot the approach,  I am not that pleased with the code solution above. I have started getting deeper into Object Oriented Programming which makes me look at the above code as difficult to follow, consisting one God method, containing many elements that depend on each other making it difficult to change later or look for a bug (there is no bug here, however)… The above code is just not clean; I am practicing ways to change this practice.

The idea of OOP is simply beautiful. Instead of having one procedural script – so called God method which does everything,  we have class object with small little parts that are responsible for one thing, and one thing only. They are independent, and we are not bothered about what is happening inside of them. Well, we know to this minimal degree where we can say what it does, not necessarily how.

If you remember my version of Tower of Hanoi published earlier, look at it now in my repository.

screen-shot-2016-10-03-at-21-28-28

It’s a class and we can make it running by loading it in IRB and instantiating a new object with i.e.,. p = TowerOfHanoi.new and then using the method on this new object t.play will run the game. It is almost like 3.even? or new_array.pop(). Yes, another method to an object, but now not like the one Ruby created (it is said by the way that everything in Ruby is an object), but object which mechanism I have designed and created… can feel sort of like Matsumoto’s assistand adding another piece to Ruby. This game is a big class, but it might be an object in another game which gives those answers that I thought it to give in the first place. It feels like powerful tool was handed in. However, difficult one.

Here is my another attempt on making Rock Paper Scissors game in OOP. This time, it consists of three classes – one for the human player, the other for the computer player and last is the game class. All saved in separate files. I tried to split them up as much as I could after devoting hours into building it. Having watched Ben Orenstein in action recently makes me want to master refactoring very much.

Another useful and exciting tasks were involving using Procs and Blocks to create a replica of each, inject, ma, reverse… methods, or even something useful like benchmark method to measure time at which specific block method works out a solution.

As for personal life, it was a great week. There were good news about my dad, he will get treatment this week, and new examinations brought more positive results. I started to turn off my phone during studying – helps a lot. I have filled out my google calendar with all the stuff I do in a week – helped me to focus on coding on Mon -Fri exclusively and afternoons on weekends. This week job keeps me busy as rarely, and I have some overtimes every day so fighting again to have every hour I can devote to studying.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s