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
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
2
It is possible using Regular Expression, checks for character types or character patterns.
Ex:
[0-9] - Apenas numeros
[a-zA-Z] - Alfabeto com letras minusculas e maiusculas
Search for variations in case you need to assemble specific patterns, it is very useful in certain scenarios.
https://guru-sp.github.io/tutorial_ruby/expressoes-regulares.html
http://diegorubin.com/2011/04/16/utilizando-expressoes-regulares-em-ruby
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 ruby-on-rails ruby
You are not signed in. Login or sign up in order to post.
For that specific case, Regular Expression is what you need to use.
– felipsmartins
I think you meant the "content type" that exists within a string variable. Check ? Change the question to be clearer.
– Fernando Vieira