[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

detecting data type

Parv G.

10/19/2006 1:03:00 PM

hello all,
i'm new to ruby and need some help on what seems like a simple issue.

How do i find out if the argument passed to a method is a string or an
integer?

Here's what i'm trying to do:

def method_test(some_arg)
if some_arg is string
#callmethod A
else
#callmethod B
end
end

Thanks for your help.

Parv


--
Posted via http://www.ruby-....

4 Answers

Thomas Adam

10/19/2006 1:09:00 PM

0

On Thu, 19 Oct 2006 22:03:07 +0900
"Parv G." <ghotrapa@yahoo.com> wrote:

> hello all,
> i'm new to ruby and need some help on what seems like a simple issue.
>
> How do i find out if the argument passed to a method is a string or
> an integer?
>
> Here's what i'm trying to do:
>
> def method_test(some_arg)
> if some_arg is string
> #callmethod A
> else
> #callmethod B
> end
> end

irb(main):002:0> 'a'.is_a?(String)
=> true

etc.

-- Thomas Adam

Parv G.

10/19/2006 1:28:00 PM

0

Thomas Adam wrote:
> On Thu, 19 Oct 2006 22:03:07 +0900
> "Parv G." <ghotrapa@yahoo.com> wrote:
>
>> #callmethod A
>> else
>> #callmethod B
>> end
>> end
>
> irb(main):002:0> 'a'.is_a?(String)
> => true
>
> etc.
>
> -- Thomas Adam

Thanks for the quick response.

--
Posted via http://www.ruby-....

Hugh Sasse

10/19/2006 5:48:00 PM

0

David Vallner

10/19/2006 8:07:00 PM

0

Parv G. wrote:
> hello all,
> i'm new to ruby and need some help on what seems like a simple issue.
>
> How do i find out if the argument passed to a method is a string or an
> integer?
>
> Here's what i'm trying to do:
>
> def method_test(some_arg)
> if some_arg is string
> #callmethod A
> else
> #callmethod B
> end
> end
>

Not repeating the obvious
duck-type-but-not-really-cause-that-says-nothing-anyway, applying the
"replace conditional with polymorphism" refactoring is probably the
cleanest solution from an OO point of view. More so because this is
trivial with Ruby's open classes, and the standard library probably has
what you need (like a type conversion method).

David Vallner