Disginguishing object types

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

On 22 May 2012 15:31, Doug J. [email protected] wrote:

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?

Probably the easiest way is:
http://ruby-doc.org/core-1.9.3/Object.html#method-i-kind_of-3F

You might also want to consider:
http://ruby-doc.org/core-1.9.3/Object.html#method-i-respond_to-3F

Using a case statement is handy for this

1.9.3-p0 :010 > x = ‘hi’
=> “hi”
1.9.3-p0 :011 > case x
1.9.3-p0 :012?> when String
1.9.3-p0 :013?> puts ‘string’ #process string
1.9.3-p0 :014?> when Array
1.9.3-p0 :015?> puts ‘array’ #process array
1.9.3-p0 :016?> default
1.9.3-p0 :017?> puts ‘huh?’
1.9.3-p0 :018?> end
string
=> nil
1.9.3-p0 :019 >

From: Jeremy W. [email protected]
Reply-To: [email protected]
Date: Tuesday, May 22, 2012 10:34 AM
To: ruby-talk ML [email protected]
Subject: Re: Disginguishing object types

On 22 May 2012 15:31, Doug J. [email protected] wrote:

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?

Probably the easiest way is:
http://ruby-doc.org/core-1.9.3/Object.html#method-i-kind_of-3F

You might also want to consider:
http://ruby-doc.org/core-1.9.3/Object.html#method-i-respond_to-3F

Ian Asaff wrote in post #1061686:

Using a case statement is handy for this

1.9.3-p0 :010 > x = ‘hi’
=> “hi”
1.9.3-p0 :011 > case x
1.9.3-p0 :012?> when String
1.9.3-p0 :013?> puts ‘string’ #process string
1.9.3-p0 :014?> when Array
1.9.3-p0 :015?> puts ‘array’ #process array
1.9.3-p0 :016?> default
1.9.3-p0 :017?> puts ‘huh?’
1.9.3-p0 :018?> end

The keyword for the default case is “else”, not “default”.

Hi,

Jeremy W. wrote in post #1061681:

Probably the easiest way is:
http://ruby-doc.org/core-1.9.3/Object.html#method-i-kind_of-3F

You might also want to consider:
http://ruby-doc.org/core-1.9.3/Object.html#method-i-respond_to-3F

To explain this a bit: In Ruby you usually don’t judge objects by their
“type” (class) but by the existance of certain methods. This is called
duck typing.

The idea behind this is that the behaviour of an object matters, not
so much the class. For example, most of the time it isn’t important if
an object is actually an array. It might as well be an instance of any
other class, as long as it behaves like an array (is enumerable,
implements the “[]” method to access elements etc.). So instead of
asking “Is this an array?”, you ask “Does this have an each method?”
etc.

However, in your case it’s probably best to explicitly check for the
Array class and the String class (or a subclass).

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.

Wow!! Thanks so much for the very thorough answers.

... doug

On 23/05/2012, at 3:03 AM, “Jan E.” [email protected] wrote:

To explain this a bit: In Ruby you usually don’t judge objects by their
“type” (class) but by the existance of certain methods. This is called
duck typing.

The idea behind this is that the behaviour of an object matters, not
so much the class.

Also, where possible, don’t check for an arbitrary method, check for a
method you need or are about to call on the object in question. This
will indicate more clearly why you care what type the objects are.

However, in your case it’s probably best to explicitly check for the
Array class and the String class (or a subclass).

Unfortunately String and Array share a lot of method names.

Henry

Oops–careless. Thanks for the correction.