Does Ruby Have Primitive Data Types?

Unlike some other programming languages (such as JavaScript, Java, C#, etc.), Ruby does not have any primitive data types. In fact, everything in Ruby is an object, as you can see in the following examples:

puts 'foo'.class #=> String
puts 1234.class #=> Integer
puts 0b101.class #=> Integer
puts 0o30071.class #=> Integer
puts 0x4d2.class #=> Integer
puts :bar.class #=> Symbol
puts true.class #=> TrueClass
puts false.class #=> FalseClass
puts 12.34.class #=> Float
puts 1.234e2.class #=> Float
puts nil.class #=> NilClass

Some other programming languages use primitive data types mainly for performance purposes (as creating objects has an additional overhead comparatively). However, Ruby opts for a purely object-oriented design, to promote ease-of-learning and familiarity (as objects are used throughout Ruby, and thus, rules applying to objects apply to all of Ruby).

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.