Finding the type of variable in ruby on rails

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 kind of variable the user is entering, if it is numbers, I do a zip search, I search the street name

    
asked by anonymous 31.12.2015 / 02:28

4 answers

2

It is possible to use Regular Expression , it checks according to character types or character patterns.

Ex:

[0-9] - Apenas numeros
[a-zA-Z] - Alfabeto com letras minusculas e maiusculas
  

Search for variations if you need to set specific patterns, it is very useful in certain   scenarios.

link

link

link

    
31.12.2015 / 15:28
1

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

use this way:

variável.kind_of? tipo

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

x.kind_of?Integer
    
31.12.2015 / 23:05
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.

    
15.02.2016 / 16:37
0

Perhaps making a comparison using .class I think .class can help you by giving the information of the required variable, that's the only comparison!

    
10.01.2016 / 03:08