Ruby Introduction

Discover the elegant programming language

💎 What is Ruby?

Ruby is a dynamic, open-source programming language with elegant syntax that is natural to read and easy to write. Created by Yukihiro Matsumoto in 1995, Ruby focuses on developer happiness and productivity.


# Simple Ruby program
puts "Welcome to Ruby!"
5.times { puts "Ruby is fun!" }
                                    

Output:

Welcome to Ruby!
Ruby is fun!
Ruby is fun!
Ruby is fun!
Ruby is fun!
Ruby is fun!

Key Ruby Features

📖

Readable Syntax

Ruby code reads like English

if age >= 18
  puts "Adult"
end
🎯

Object-Oriented

Everything is an object in Ruby

5.times { puts "Hi" }
"hello".upcase
🔧

Flexible

Multiple ways to solve problems

# Both work!
puts "Hello"
p "Hello"
📦

Rich Libraries

Thousands of gems available

require 'date'
puts Date.today

🔹 Ruby Philosophy

Ruby follows the principle of least surprise and emphasizes human needs over computer needs. The language is designed to make programmers happy and productive.

Core Principles:

  • Simplicity: Code should be easy to read and write
  • Flexibility: Multiple ways to accomplish tasks
  • Expressiveness: Say more with less code
  • Natural: Syntax that feels intuitive

🔹 Your First Ruby Program

Let's write a simple Ruby program that demonstrates basic features:

# Variables and output
name = "Ruby"
version = 3.2

puts "Hello from #{name}!"
puts "Version: #{version}"
puts "#{name} makes coding fun!"

Output:

Hello from Ruby!
Version: 3.2
Ruby makes coding fun!

🔹 Ruby is Object-Oriented

In Ruby, everything is an object, even numbers and strings. This makes the language consistent and powerful:

# Numbers are objects
puts 10.class        # Integer
puts 10.even?        # true
puts 10.next         # 11

# Strings are objects
puts "hello".class   # String
puts "hello".length  # 5
puts "hello".upcase  # HELLO

Output:

Integer
true
11
String
5
HELLO

🔹 Ruby Use Cases

Ruby is versatile and used in many areas of software development:

🔸 Web Development

# Ruby on Rails example
class User
  def greet
    "Welcome to our website!"
  end
end

user = User.new
puts user.greet

Output:

Welcome to our website!

🔸 Automation Scripts

# File processing
files = ["doc1.txt", "doc2.txt", "doc3.txt"]

files.each do |file|
  puts "Processing #{file}..."
end

Output:

Processing doc1.txt...
Processing doc2.txt...
Processing doc3.txt...

🔹 Ruby vs Other Languages

See how Ruby compares with its clean, expressive syntax:

Printing "Hello" 5 times:

Ruby:

5.times { puts "Hello" }

Other languages typically need:

for(int i = 0; i < 5; i++) {
    print("Hello");
}

🧠 Test Your Knowledge

Who created the Ruby programming language?