detecting data type

P

Parv G.

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
 
T

Thomas Adam

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
 
H

Hugh Sasse

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
 
D

David Vallner

--------------enigC13127638AEFE392115ECF62
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
hello all,
i'm new to ruby and need some help on what seems like a simple issue.
=20
How do i find out if the argument passed to a method is a string or an =
integer?
=20
Here's what i'm trying to do:
=20
def method_test(some_arg)
if some_arg is string
#callmethod A
else
#callmethod B
end
end
=20

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




--------------enigC13127638AEFE392115ECF62
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFN9rEy6MhrS8astoRAgxCAJ40MhnkHIOOXtFeJUaFQKv0TEPMhwCdHDko
SxcHN7v68Ii3KeDig3BLbwI=
=jPaA
-----END PGP SIGNATURE-----

--------------enigC13127638AEFE392115ECF62--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,215
Messages
2,571,113
Members
47,713
Latest member
LeliaB1379

Latest Threads

Top