AnyCompiler

Introduction to Ruby

Ruby is a dynamically typed, interpreted, fully object-oriented scripting language. "Fully" is doing real work in that sentence: in Ruby, even an integer is an object. 5.times { ... } is a method call on the number 5. nil.nil? is a method call on nil itself. There is no such thing as a "primitive" that sits outside the object system — everything you touch is an instance of some class, and every class is open to inspection and extension at runtime.

Ruby is frequently filed next to Perl and Python — all three are dynamically typed scripting languages with no compile step — but three features make Ruby meaningfully its own thing rather than a reskin of either: blocks (its own do...end/{ } syntax for passing code into a method call, distinct from a lambda literal), symbols (:name, a lightweight immutable identifier type with no equivalent in Python, JS, Go, Rust, or the C-family), and mixins via module/include (a multiple-inheritance-like composition model unlike Java/C#/Kotlin's single-inheritance-plus-interfaces or Rust's traits). This course treats all three as first-class topics, not footnotes.

A quick preview

Here's a real Ruby program, executed for real on this page's own compiler — not a mockup. It touches three of Ruby's defining ideas at once: a block passed to times, a symbol, and nil responding to a method call.

puts "Ruby in action:"
5.times { |i| print i, " " }
puts
puts :ruby.class
puts nil.nil?
Output
Ruby in action:
0 1 2 3 4 
Symbol
true

5.times { |i| ... } isn't a loop keyword — times is an ordinary method on the Integer 5, and { |i| ... } is a block, a chunk of code handed directly to that method call. :ruby.class returns Symbol, a type with no real counterpart in Python, JavaScript, Rust, Go, or any C-family language on this site. And nil.nil? works because nil — Ruby's absence-of-a-value — is itself an object (an instance of NilClass) that responds to methods, the same as any other value. Next: your first program — puts, print, and running top to bottom with no compile step.

Practice exercise

Print the class of 5, then the class of :ruby, then the class of nil, one per line.