To install ruby:

# Update system packages
sudo apt update && sudo apt upgrade -y

# Install dependencies
sudo apt install -y git curl autoconf bison build-essential libssl-dev libyaml-dev libreadline-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm-dev libgdbm6

# Install rbenv
curl -fsSL <https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-installer> | bash

# Add rbenv to PATH (add to ~/.bashrc or ~/.zshrc)
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc

# Install Ruby 3.2
rbenv install 3.2.2
rbenv global 3.2.2

# Verify
ruby -v  # Should output "ruby 3.2.2"

To install Rails:

#Install rails
gem install rails

#Verify
rails -v

Ruby basics:


# Strings
name = "Alice"
puts "Hello, #{name}!" # String interpolation

# Numbers
age = 25
price = 19.99

# Booleans
is_adult = true
is_child = false

# Arrays
fruits = ["apple", "banana", "orange"]
puts fruits[0] # "apple"

# Hashes (Key-Value Pairs)
person = { name: "Bob", age: 30 }
puts person[:name] # "Bob"

# if-else
age = 18
if age >= 18
  puts "Adult"
else
  puts "Child"
end

# case-when
grade = 'B'
case grade
when 'A' then puts "Excellent!"
when 'B' then puts "Good!"
else puts "Try harder!"
end

# each (Preferred in Ruby)
fruits = ["apple", "banana", "orange"]
fruits.each do |fruit|
  puts fruit
end

# while loop
i = 0
while i < 5
  puts i
  i += 1
end

# for loop (Rarely used in Ruby)
for i in 1..3
  puts i
end

NOTE:

Ruby is interpreted, but it uses a mix of interpretation + Just-In-Time (JIT) compilation in modern versions (MRI 3.x+).

JIT (Just-In-Time Compilation) & MRI (Matz's Ruby Interpreter) • Reads Ruby code → converts to bytecode → executes it.

first step: ruby script.rb # Uses MRI by default

second step: ruby --jit script.rb # Faster for long-running programs

Term Meaning Role in Ruby
MRI Default Ruby engine Interprets Ruby code
JIT Optimization technique Compiles hot code to machine code

Example to understand:

Structure of Ruby:

  1. Lexical Structure: The Ruby interpreter parses a program as a sequence of tokens. Tokens include comments, literals, punctuation, identifiers, and keywords.
  2. Comments Starts with #.
  3. Embedded Documents: use =begin and =end for using Multiline comments!