Web Development
0/6 sections
0%

Ruby

Ruby is an expressive, object-oriented language famous for developer happiness. It powers Ruby on Rails, one of the most productive web frameworks ever created, and follows the principle of least surprise.

1Ruby Fundamentals

ruby
# Variables
name = "Sufi"         # local
@instance_var = 42    # instance
@@class_var = 0       # class
CONSTANT = 3.14       # constant

# Everything is an object 42.class # Integer "hello".class # String nil.class # NilClass true.respond_to?(:to_s) # true

# String interpolation puts "Hello, #{name}!" puts "2 + 2 = #{2 + 2}"

# Symbols (immutable, memory-efficient strings) status = :active { name: "Sufi", role: :admin } # symbol keys

# Ranges (1..10).to_a # [1, 2, ..., 10] inclusive (1...10).to_a # [1, 2, ..., 9] exclusive ('a'..'z').include?('m') # true ```

2Collections & Blocks

ruby
# Arrays
fruits = ['apple', 'banana', 'cherry']
fruits.push('date')         # or fruits << 'date'
fruits.first                # 'apple'
fruits.last                 # 'date'
fruits.length               # 4

# Hashes person = { name: "Sufi", age: 30, role: :admin } person[:name] # "Sufi" person.keys # [:name, :age, :role] person.merge(email: "s@example.com")

# Blocks, Procs, Lambdas [1, 2, 3].map { |n| n * 2 } # [2, 4, 6] [1, 2, 3, 4].select { |n| n.even? } # [2, 4] [1, 2, 3].reduce(0) { |sum, n| sum + n } # 6 [1, 2, 3].each_with_object([]) { |n, acc| acc << n * 2 }

# Chaining (1..100) .select(&:odd?) .map { |n| n ** 2 } .first(5) # [1, 9, 25, 49, 81] ```

3OOP in Ruby

ruby
class Animal
  attr_accessor :name, :sound  # creates getter + setter
  attr_reader :id               # getter only

@@count = 0 # class variable

def initialize(name, sound) @name = name @sound = sound @id = SecureRandom.uuid @@count += 1 end

def speak "#{@name} says #{@sound}!" end

def self.count # class method @@count end

def to_s "Animal(#{@name})" end end

class Dog < Animal def initialize(name) super(name, "woof") # call parent end

def fetch(item) "#{@name} fetches the #{item}!" end end

rex = Dog.new("Rex") puts rex.speak # Rex says woof! puts Animal.count # 1 ```

4Modules & Mixins

ruby
module Greetable
  def greet(visitor)
    "Hello, #{visitor}! I'm #{name}."
  end
end

module Serializable def to_json vars = instance_variables.map do |var| [var.to_s.delete('@'), instance_variable_get(var)] end "{#{vars.map { |k,v| '"' + k + '": "' + v.to_s + '"' }.join(', ')}}" end end

class Person include Greetable include Serializable

attr_reader :name, :email

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

p = Person.new("Sufi", "s@example.com") puts p.greet("Alice") # Hello, Alice! I'm Sufi. puts p.to_json ```

5Ruby on Rails

ruby
# Model (app/models/user.rb)
class User < ApplicationRecord
  has_many :posts, dependent: :destroy
  has_many :comments
  belongs_to :team, optional: true

validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP } validates :name, presence: true, length: { minimum: 2, maximum: 50 }

has_secure_password # bcrypt password hashing

scope :active, -> { where(active: true) } scope :recent, -> { order(created_at: :desc).limit(10) }

before_create :generate_token

private def generate_token self.token = SecureRandom.urlsafe_base64 end end

# Controller (app/controllers/users_controller.rb) class UsersController < ApplicationController before_action :authenticate_user! before_action :set_user, only: [:show, :update, :destroy]

def index @users = User.active.recent.includes(:posts) render json: @users end

def create @user = User.new(user_params) if @user.save render json: @user, status: :created else render json: @user.errors, status: :unprocessable_entity end end

private def set_user = @user = User.find(params[:id]) def user_params = params.require(:user).permit(:name, :email, :password) end ```

6Metaprogramming & DSLs

ruby
# method_missing intercept undefined methods
class DynamicFinder
  def method_missing(name, *args)
    if name.to_s.start_with?('find_by_')
      field = name.to_s.sub('find_by_', '')
      puts "Searching by #{field}: #{args.first}"
    else
      super
    end
  end

def respond_to_missing?(name, include_private = false) name.to_s.start_with?('find_by_') || super end end

finder = DynamicFinder.new finder.find_by_email("user@example.com") # Searching by email: user@example.com

# define_method generate methods dynamically class Logger %w[debug info warn error].each do |level| define_method(level) do |message| puts "[#{level.upcase}] #{Time.now}: #{message}" end end end

# DSL example class RailsRoute def get(path, to:) puts "GET #{path} → #{to}" end end ```

Explore the Full Portfolio

This is the complete portfolio of Sufi Khan Sulaiman, a technology leader specialising in B2B commerce and digital automation. Start from the Home page for the overview, then move through two decades of career experience across FLIR Systems, Lorex Technology, and 1c Platform, and the full catalogue of project case studies spanning headless commerce migrations, AI recommendation engines, and multi-channel fulfilment systems.

The skills and certifications page maps the technical and leadership capabilities behind the work, while the articles and the knowledge base break down the thinking into actionable frameworks. For hands-on learning, the tutorials and applications sections cover practical builds from front-end fundamentals to full-stack web apps.

For consulting engagement, the expertise page outlines service offerings, the ecommerce hub covers platform architecture and automation strategy, and the ecommerce guide (PDF) is a downloadable 55-page field manual. When you are ready to talk, the contact page is the direct line.