From: Michel Date: 2012-05-23T00:55:16+09:00 Subject: Re: Disginguishing object types --=-thUygufKMC0Oq/uf9E7A Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit > Frequently hash values are a mixture of object types. In the case at > issue, the various values of a particular hash may be either strings or > an array of strings. As I iterate through the hash to process the > respective values, I need to process a string value differently from the > way that I process a value that is an array. My question is: How do I > determine the object type of the respective values so that I can channel > it to the proper processing mechanism? > > I apologize. I'm sure that I should be able to find the answer to this > question; but, I don't know what search criteria to use. > > Thanks for any input. > > ...doug > Because Ruby allows dynamic typing, you may use polymorphism. To achieve it, you have simply to define methods with the same name for each type and ruby will call the correct method according to the class of each object. You don't need any if or case statement to iterate on the hash and if this hash may contain a new type all you have to do is to define a method for this new class. For example, all objects respond to the to_s method because at the top of the hierarchy the Object class has a to_s default method. But if you define a particular to_s method for one of your own classes, it is this method that will be called for any object of that class... and the inspect method will call it too. Regards, Michel. --=-thUygufKMC0Oq/uf9E7A Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: 7bit
Frequently hash values are a mixture of object types.  In the case at
issue, the various values of a particular hash may be either strings or
an array of strings.  As I iterate through the hash to process the
respective values, I need to process a string value differently from the
way that I process a value that is an array.  My question is:  How do I
determine the object type of the respective values so that I can channel
it to the proper processing mechanism?

I apologize.  I'm sure that I should be able to find the answer to this
question; but, I don't know what search criteria to use.

Thanks for any input.

     ...doug

Because Ruby allows dynamic typing, you may use polymorphism.
To achieve it, you have simply to define methods with the same name for each type and ruby will call the correct method according to the class of each object. You don't need any if or case statement to iterate on the hash and if this hash may contain a new type all you have to do is to define a method for this new class.

For example, all objects respond to the to_s method because at the top of the hierarchy the Object class has a to_s default method. But if you define a particular to_s method for one of your own classes, it is this method that will be called for any object of that class... and the inspect method will call it too.

Regards,
Michel. --=-thUygufKMC0Oq/uf9E7A--