# Detecting data type

**URL:** <https://www.ruby-forum.com/t/detecting-data-type/72865>\
**Category:** Ruby\
**Created:** [October 19, 2006, 3:03pm UTC](https://www.ruby-forum.com/t/detecting-data-type/72865 "2006-10-19T15:03:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [October 19, 2006, 3:03pm UTC](https://www.ruby-forum.com/t/detecting-data-type/72865/1 "2006-10-19T15:03:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [October 19, 2006, 3:10pm UTC](https://www.ruby-forum.com/t/detecting-data-type/72865/2 "2006-10-19T15:10:41Z")

</div>

On Thu, 19 Oct 2006 22:03:07 +0900  
“Parv G.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> #callmethod A
> 
> ```
> 
> else  
> #callmethod B  
> end  
> end

irb(main):002:0\> ‘a’.is\_a?(String)  
=\> true

etc.

– Thomas A.

---

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [October 19, 2006, 3:28pm UTC](https://www.ruby-forum.com/t/detecting-data-type/72865/3 "2006-10-19T15:28:14Z")

</div>

Thomas A. wrote:

> On Thu, 19 Oct 2006 22:03:07 +0900  
> “Parv G.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ```
> > #callmethod A
> > 
> > ```
> > 
> > else  
> > #callmethod B  
> > end  
> > end
> 
> irb(main):002:0\> ‘a’.is\_a?(String)  
> =\> true
> 
> etc.
> 
> – Thomas A.

Thanks for the quick response.

---

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [October 19, 2006, 7:49pm UTC](https://www.ruby-forum.com/t/detecting-data-type/72865/4 "2006-10-19T19:49:39Z")

</div>

On Thu, 19 Oct 2006, 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?

“It depends” 🙂 At the most basic you could look at the class:  
x.class  
and see what that gives you, but classes are open in Ruby so it  
might not be the beast it was at birth. So you really want to  
know if it has the right properties for what you are doing. So you  
could use  
x.respond\_to?(:dowcase)  
but that really doesn’t tell you much, because the method could be  
redefined. In short, you try not to test this sort of thing.  
Searth the web for “Duck typing” for more on this issue.

> 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 really, that’s more like what you have done when trying to do  
something else. What was your actual goal?  
For this case you _could_ use:

if some\_arg.respond\_to?(:method\_a)  
some\_arg.method\_a  
else  
some\_arg.method\_b  
end

but ideally you want to be sure that your method is called with the  
correct type anyway. You could also use some\_arg.to\_i, some\_arg.to\_s  
to convert to the correct type.

> Thanks for your help.
> 
> Parv

```
    Hugh
```

---

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [October 19, 2006, 10:07pm UTC](https://www.ruby-forum.com/t/detecting-data-type/72865/5 "2006-10-19T22:07:15Z")

</div>

Parv G. wrote:

> ```
> #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 V.
