Ruby: Value Types

By Xah Lee. Date: .

In Ruby, variables do not need declaration. Variable does not have a type. Values do have type. The type is the class they belong to.

Finding the type of a value.

# ruby

# find the type
# use the class method

p 5.class == Integer
p 5.6.class == Float
p "5".class == String
p [3,4,5].class == Array
p true.class == TrueClass
p false.class == FalseClass
p nil.class == NilClass
# ruby

# check a value's type

# kind_of? is a method that returns true or false
p 5.kind_of?(Integer)
p 5.6.kind_of?(Float)
p "5".kind_of?(String)
p [3,4,5].kind_of?(Array)
p true.kind_of?(TrueClass)
p false.kind_of?(FalseClass)
p nil.kind_of?(NilClass)

Ruby, List Classes and Methods