Category: Examples

  • File Handling

    ontent
    “w” → write mode (creates file if not exist).

    File.read → reads entire file content.

    Output in console:


    Hello Ruby File!
    And file test.txt is created with that text.

  • Blocks, Procs, Lambdas

    Block

    3.times { puts “Ruby is fun!” }

    Proc

    say_hello = Proc.new { |name| puts “Hello, #{name}” }
    say_hello.call(“Sara”)

    Lambda

    square = -> (x) { x * x }
    puts square.call(5)
    Block → Small piece of code in {} or do…end.

    Proc → Stored block that can be reused.

    Lambda → Like Proc but stricter with arguments.

    Output:


    Ruby is fun!
    Ruby is fun!
    Ruby is fun!
    Hello, Sara
    25

  • Classes and Objects

    class Person
    attr_accessor :name, :age # creates getter & setter

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

    def introduce
    “Hi, I’m #{@name} and I’m #{@age} years old.”
    end
    end

    p1 = Person.new(“Ali”, 25)
    puts p1.introduce
    class defines a blueprint.

    @name and @age are instance variables.

    initialize runs when object is created.
    Output:


    Hi, I’m Ali and I’m 25 years old.

  • Methods

    def greet(name)
    return “Hello, #{name}!”
    end

    puts greet(“Ayesha”)
    def starts a method, end closes it.

    return gives back a value.
    Output:


    Hello, Ayesha!

  • Loops

    While Loop

    i = 1
    while i <= 5
      puts i
      i += 1
    end
    
    • Runs while condition is true.
      Output:
    1
    2
    3
    4
    5

    For Loop

    for n in 1..5
      puts n
    end
    
    • 1..5 means range from 1 to 5.
      Output: 1 2 3 4 5

    Each Loop

    [1, 2, 3, 4, 5].each do |num|
      puts num * 2
    end
    
    • Iterates over each element.
      Output:
    2
    4
    6
    8
    10
    

  • Conditions

    age = 18

    if age >= 18
    puts “You are an adult.”
    elsif age >= 13
    puts “You are a teenager.”
    else
    puts “You are a child.”
    end
    if … elsif … else … end is used for decision making.
    Output:


    You are an adult.

  • Hashes (Dictionary / Map)

    student = { “name” => “Ali”, “age” => 22, “grade” => “A” }
    puts student[“name”] # Ali
    student[“age”] = 23 # update
    puts student.inspect
    Hashes store key-value pairs.

    Keys can be strings “name” or symbols :name.
    Output:


    Ali
    {“name”=>”Ali”, “age”=>23, “grade”=>”A”}

  • Arrays

    fruits = [“apple”, “banana”, “cherry”]

    puts fruits[0] # apple
    puts fruits.length # 3
    fruits << “mango” # Add new element
    puts fruits.inspect # Show complete array
    Arrays store multiple values.

    Index starts from 0.

    << means “append”.

    Output:


    apple
    3
    [“apple”, “banana”, “cherry”, “mango”]

  • Variables and Data Types

    name = “Saim” # String
    age = 25 # Integer
    height = 5.9 # Float
    is_student = true # Boolean

    puts “Name: #{name}, Age: #{age}, Height: #{height}, Student: #{is_student}”
    Ruby is dynamically typed → no need to mention string, int, etc.

    {} → used inside strings for interpolation (inserting values).

    Output:


    Name: Saim, Age: 25, Height: 5.9, Student: true

  • Hello World

    puts “Hello, World!”
    puts → Prints text with a new line at the end.

    “Hello, World!” is a string.
    Output:


    Hello, World!