From: Ian Asaff Date: 2012-05-22T23:52:18+09:00 Subject: Re: Disginguishing object types > This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. --B_3420528731_372667 Content-type: text/plain; charset="US-ASCII" Content-transfer-encoding: 7bit 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 Walker Reply-To: Date: Tuesday, May 22, 2012 10:34 AM To: ruby-talk ML Subject: Re: Disginguishing object types On 22 May 2012 15:31, Doug Jolley 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 > > 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 > > -- > Posted via http://www.ruby-forum.com/. > --B_3420528731_372667 Content-type: text/html; charset="US-ASCII" Content-transfer-encoding: quoted-printable
Using a case statement = is handy for this

1.9.3-p0 :010 > x =3D 'hi'
=
 =3D> "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
&nb= sp;=3D> nil 
1.9.3-p0 :019 > 

From: Jeremy Walker <jez.walker@gmail.com>
Reply-To: <= ruby-talk@ruby-lang.org>
Date: Tuesday, May 22, 2012 10:34 AM
To: ruby-talk ML <ruby-talk@rub= y-lang.org>
Subject: Re: Di= sginguishing object types



On 22 May 2012 15:31, Doug Jolley <lists@ruby-forum.com> w= rote:
Frequently hash values are a mixture of object types.  In the case at<= br> 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:  Ho= w 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:

You might also want to c= onsider:
 

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

Thanks for any input.

    ...doug

--
Posted via http://www.= ruby-forum.com/.


--B_3420528731_372667--