Friday, June 17, 2011

Changing existing classes

Getting through that sort method felt pretty good, and I feel much more confident about my writing now than I did a few days ago. Today I wrote up the bubble_shuffle method, but to use some new knowledge as well from reading through the rest of Chris Pine's book, I just modified the Array class to include my shuffle method, despite there already being a shuffle method defined in the class.

Classes make up the big picture. Even though Ruby counts EVERYTHING as an object (and boy do they mean everything!), classes make up those objects. objects have different classes. For example, Integers are a class, Arrays are a class, Hashes (which are like arrays) are a class. Each class has a built in number of methods, and to my knowledge is ever increasing as Ruby gets updated, but you are allowed to edit classes yourself to write your own methods. Pine gives the example of to_eng for the Integer class, which translates integers into written form, ie. 3.to_eng returns 'three'.

Originally he wanted us to just write a shuffle method in the recursion chapter, but I just went ahead and skipped to the newer exercise, which modifies the Array class to include my shuffler. Now I can put in an array and use the method to have it shuffled and returned.



I saw on rubyquiz.com that there's a solitaire cipher quiz. I think I'll work on that one tonight. I definitely feel confident enough to figure that one out on my own, or at least understand the solutions. In the meantime, I have another job application to submit tonight. Until much later tonight, rubyers!

No comments:

Post a Comment