Thuta Learning
BasicProgrammingbeginner

Data Types

Relax. We'll talk through this in plain words — no textbook voice.

Understanding Ruby's data types makes it much easier to use methods, check conditions, and work with collections. The most commonly used ones in Ruby are String, Integer, Float, Boolean, Nil, Array, and Hash.

ruby
name = "Ruby"       # String
age = 25           # Integer
price = 10.99      # Float
is_fun = true      # TrueClass
nothing = nil      # NilClass

puts name.class
puts age.class
puts price.class
puts nothing.nil?

.class shows an object's class/type. nil? returns true or false depending on whether the value is nil.

You should see
String Integer Float true

Info

nil means the absence of a value. It's not the same as an empty string "".

Easy traps

  • Don't confuse nil with the string "nil". nil means there's no value, while "nil" is just text.
Data Types | Thuta Learning