Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Comprehensive Ruby Tutorial

Introduction to Ruby

Ruby is a dynamic, open-source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.

Installation

To install Ruby, follow these steps:

Install Ruby using a version manager like RVM:

$ \curl -sSL https://get.rvm.io | bash -s stable --ruby

Or install Ruby using a package manager like Homebrew on macOS:

$ brew install ruby

Basic Syntax

Let's start with a simple "Hello, World!" program in Ruby:

puts 'Hello, World!'

Save the above code in a file named hello.rb and run it using the following command:

$ ruby hello.rb
Hello, World!

Variables

In Ruby, variables are used to store data. Here is an example:

name = 'Alice'
age = 30
puts "Name: #{name}, Age: #{age}"

The output will be:

Name: Alice, Age: 30

Data Types

Ruby supports several data types, including:

  • Numbers: Integers and floating-point numbers
  • Strings: Sequence of characters
  • Arrays: Ordered, integer-indexed collections of objects
  • Hashes: Collections of key-value pairs

Here are some examples:

num = 42
pi = 3.14
greeting = 'Hello'
colors = ['red', 'green', 'blue']
person = {'name' => 'Alice', 'age' => 30}

Control Structures

Ruby provides several control structures, including if statements, loops, and iterators.

If Statements

age = 18
if age >= 18
  puts 'You are an adult.'
else
  puts 'You are a minor.'
end

Loops

5.times do |i|
  puts "Iteration #{i}"
end

Iterators

colors = ['red', 'green', 'blue']
colors.each do |color|
  puts color
end

Methods

Methods in Ruby are used to bundle one or more repeatable statements into a single unit. Here's an example:

def greet(name)
  "Hello, #{name}!"
end
puts greet('Alice')
Hello, Alice!

Classes and Objects

Ruby is an object-oriented language. Everything in Ruby is an object, and classes define the blueprint for objects. Here's an example:

class Person
  attr_accessor :name, :age

  def initialize(name, age)
    @name = name
    @age = age
  end

  def greet
    "Hello, my name is #{@name} and I am #{@age} years old."
  end
end

person = Person.new('Alice', 30)
puts person.greet
Hello, my name is Alice and I am 30 years old.

Modules

Modules in Ruby are used to group related methods. Modules cannot create instances. Here's an example:

module Greetings
  def greet(name)
    "Hello, #{name}!"
  end
end

class Person
  include Greetings
end

person = Person.new
puts person.greet('Alice')
Hello, Alice!

Exception Handling

Ruby provides a mechanism for handling errors through exception handling. Here's an example:

begin
  # Risky operation
  result = 10 / 0
rescue ZeroDivisionError => e
  puts "Error: #{e.message}"
end
Error: divided by 0

Working with Kafka in Ruby

Apache Kafka is a distributed streaming platform. To integrate Kafka with Ruby, you can use the ruby-kafka gem. Here's how to get started:

Installation

Add the following line to your Gemfile:

gem 'ruby-kafka'

Then run:

$ bundle install

Producing Messages

require 'kafka'

kafka = Kafka.new(seed_brokers: ['kafka://localhost:9092'])

producer = kafka.producer

producer.produce('Hello, Kafka!', topic: 'greetings')

producer.deliver_messages

Consuming Messages

require 'kafka'

kafka = Kafka.new(seed_brokers: ['kafka://localhost:9092'])

consumer = kafka.consumer(group_id: 'my-group')

consumer.subscribe('greetings')

consumer.each_message do |message|
  puts message.value
end