On Thu, 19 Oct 2006, Parv G. wrote:
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
“It depends” At the most basic you could look at the 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
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:
if some_arg is string
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:
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.