Things that do not exist but exist in Ruby


I’ve written object oriented programs in PHP and Python and one of the major things known about Object oriented programming is the existence of constructors and maybe destructors. We curse PHP for not being complete yet its OOP consist of this. In Python when building GUI programs, I call slots and signals from different methods that all share something in common which is specified in the __init__ method (our constructor). Python didn’t have to call it _constructor like PHP does but it exists anyway.

I’ve been working with rails lately and I’ve had to write ruby which I totally enjoy. However, there are some things in Ruby that exist just as patches not like a core feature provided by the language. I will only be talking about two things but I’ll update this article in the future when I find more.
http://geoxinfo.blogspot.com/search/label/Tutorials

Constructors

I spoke of constructors in Python and PHP because I will like to point at the fact that there are really no such things as constructors in ruby. To achieve making a variable available to all methods in a class, you have to go through the following which is a bit hacky

 Hash Item Deletion

PHP doesn’t really distinguish index arrays from hash-type arrays. They are all called arrays. By index arrays, I mean arrays written this way
And by Hash-Type Arrays, I mean this
For the index arrays, the indexes are the keys, and the Hash-type, the key as used is the key. To delete a specific item from an array in PHP, you will unset the key like so :
In Python, Hashes are known as dictionaries and we can create one this way
To remove an item from this dictionary
In Ruby, a simple Hash
There’s a delete method to call on a hash that takes the key to be deleted
it’s weird how it returns the value of j. If it was to return anything at all, it should be what’s left after ‘j’ is removed. But then I must mention that myHash has changed and it no longer contains the ‘j’ item. When considering convention in ruby with methods like upcase, capitalize, gsub, merge, we can have them to strictly keep the change by adding an exclamation (!) like
but this wouldn’t work with delete. If the delete method was returning what is expected then it must likely would. Conclusively the delete is unreliable for the job.
So how do we remove an item from Hash in ruby? You have to build your way around it. So we have to write our own class like:
Which now makes it possible to do the following on our Hash

Now that took off our ‘j’ item as expected. To keep the change to the myHash variable we can have it as most of the other methods with a bang
When using rails, you don’t need to re-write this class as rails already provides this but I think it should be built into Ruby. To use this while debugging your app from the console, you can use it with pry. You can bind pry with rails by running within your application
I find the most useful when updating many attributes from the update method and I have some attributes I don’t want to send to the database. Like:


Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment