2

I have an issue checking variable types in ruby. Here is a sample code of python that I would like to replicate in ruby. I would like to check the input type: whether string, int, or list, and proceed to a specific printing action.

def printing (input):
    if type(input) == type(""):
        pass
    elif type(input) == type(1):
        pass
    elif type(input) == type([]):
        pass
    elif type(input) == type({}):
        pass
    elif type(input) == type(()):
        pass

I cannot find a method that will do this in ruby. The code below is what I want it to look like. I am assuming that I have to check the type at the case stage.

def printing (element)
    case element
    when element.type("")
        puts element
    when element.type(2)
        puts element
    when element.type({})
        element.each_pair { |name, val|  print "#{name} : #{value}"}
    when element.type([])
        element.each {|x| print x}
    end
end
sawa
  • 165,429
  • 45
  • 277
  • 381
yveskas
  • 35
  • 1
  • 6
  • Your question is unclear. Variables don't have types in Ruby, so you simply cannot check them. I am not familiar enough with Python to know how Python does it, but in Ruby, it's simply impossible. The information you want is not there. – Jörg W Mittag Dec 30 '15 at 10:56
  • There *was* a research project called *Diamondback Ruby (DRuby)* adding a type system to Ruby about 6–7 years ago, maybe that is what you are looking for? [*Diamondback Ruby (DRuby) is an extension to Ruby that aims to bring the benefits of static typing to Ruby without compromising the expressiveness of the language.*](http://cs.umd.edu/projects/PL/druby/) – Jörg W Mittag Dec 30 '15 at 11:04
  • @JörgWMittag variables don't have types in Ruby, but the values they are bound to do. Python is identical to Ruby in this regard. – Wayne Conrad Dec 30 '15 at 13:07
  • Try to avoid this, Ruby is an OOP language so classes and objects should know how to respond to messages. – alebian Dec 30 '15 at 20:07

2 Answers2

8

I think you are looking for Object#class. Here:

element = {}
element.class
# => Hash
a = []
a.class
# => Array

This will make your switch case as follows:

case element
when String
 # do something
when Fixnum
 # do something
when Hash
 # do something
when Array
 # do something
end

Note: As mentioned by @ndn in comments below, case statement should not have .class in it (which I had initially in my answer). You can find the explanation here.

Community
  • 1
  • 1
shivam
  • 16,048
  • 3
  • 56
  • 71
  • @ndn Please undelete your answer. It is not the same as this. – sawa Dec 30 '15 at 08:53
  • Thank you that helps a lot. I have been searching for a solution all day and reading docs. – yveskas Dec 30 '15 at 08:55
  • As of now, this answer became correct, but that is due to an edit made six minutes after ndn's original answer, which had been correct. – sawa Dec 30 '15 at 08:55
2

This is not the "correct answer", I would simply like to point out that you should not use type in python you should use isinstance instead

isinstance(input, list) # test for list
isinstance(inpit, [float, int]) # test for number

if you are using python 3 you can check for abstract base classes

import collections
isinstance(input, collections.abs.Sequence) # sequence = tuple, list and a lot of other stuff that behaves that way
jcr
  • 1,015
  • 6
  • 18