๐ŸŽ“ A1Learner
๐Ÿ”

Ruby Data Types

A data type is the kind of a value. Each value in Ruby belongs to one kind.

Whole and decimal numbers

The .class call after each number asks for its type. The two numbers print two different type names.

puts 42.class
puts 3.5.class

Text is a String

Both values here are text in quotes. Each one prints String as its type.

puts "Ana".class
puts "42".class

true and false

true and false are the boolean values. Each one prints its own class name.

puts true.class
puts false.class

The nil value

nil means no value at all. Its class name prints as NilClass.

puts nil.class

Example

puts 7.class
puts 2.5.class
puts "Mina".class
puts false.class
puts nil.class

Key points

  • A data type is the kind of a value.
  • The Integer type holds whole numbers.
  • The Float type holds numbers with a decimal part.
  • The String type holds text.
  • true and false are the boolean values.
  • nil means no value.
  • Writing .class after a value asks for the name of its type.

In this example, five values ask for their class in order. The whole number gives Integer and the decimal number gives Float. The text gives String, false gives FalseClass, and nil gives NilClass.

Note

Text in quotes is a String, even when the text holds only digits.

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท Programiz Ruby Compiler

Exercise

What does puts 42.class print?