AnyCompiler

Everything Is an Object

In Python, JavaScript, Go, Rust, and the entire C-family on this site, there's a hard line between "primitive values" (numbers, booleans) and "real objects" you call methods on. Ruby has no such line. 5 is an instance of Integer. true is an instance of TrueClass. nil is an instance of NilClass. All of them inherit from Object, and all of them respond to method calls exactly like a string or a custom class instance would.

puts 5.times.to_a.inspect
puts 5.class
puts nil.nil?
puts nil.class
puts "hello".class
puts 42.respond_to?(:+)
Output
[0, 1, 2, 3, 4]
Integer
true
NilClass
String
true

5.times with no block returns an Enumerator — itself an object — which .to_a then converts into a concrete array. respond_to?(:+) asks 42 (an Integer object) whether it understands a message named +, and it does, because + is implemented as an ordinary method on Integer, not special compiler syntax. This is the foundation the rest of this course builds on: methods, blocks, and mixins all make sense once you accept that there's no value in Ruby that sits outside the method-call system. Next: control flow — if/elsif/unless as expressions with a real value, plus Ruby's own undefined-method error.

Practice exercise

Convert 3.times into an array with .to_a and print it with .inspect, then print nil.nil?.