Find out the type of variable in ruby on Rails

Asked

Viewed 1,365 times

0

I have a form in search Rails that can receive a zip code or the name of a street, so I need to know what type of variable the user is entering, if it is numbers, I do a search by zip code, if it is text, I do a search by street name

  • For that specific case, Regular Expression is what you need to use.

  • I think you meant the "content type" that exists within a string variable. Check ? Change the question to be clearer.

4 answers

2

1

To find out the type of the variable use the method kind_of?, because it returns a boolean value.

use that way:

variável.kind_of? tipo

Example to find out if "x" is integer type:

x.kind_of?Integer
  • Can you format what is code than it is not in the answer? So it becomes clearer.

0

Perhaps making a comparison using the .class I think the .class can help you by giving you the information of the required variable, so just make the comparison!

0

irb(main):006:0> Integer = 1
(irb):6: warning: already initialized constant Integer

=> 1

irb(main):007:0> 1.class

=> Fixnum

This would be a way to find out the type of the variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.