Category: Projects

  • Number Guessing Game

    Random numbers, Loops, Conditionals

    number = rand(1..10)
    attempts = 0
    
    puts "Guess the number (1-10):"
    
    loop do
      guess = gets.chomp.to_i
      attempts += 1
    
      if guess == number
    
    puts "🎉 Correct! You guessed it in #{attempts} tries."
    break
    elsif guess < number
    puts "Too low! Try again."
    else
    puts "Too high! Try again."
    end end

    Explanation:

    • rand(1..10) generates random number.
    • Keeps track of attempts.
    • Gives hints (too low, too high).
  • File Based Notes App

    File Handling, Loops

    FILENAME = "notes.txt"
    
    loop do
      puts "\n--- Notes App ---"
      puts "1. Write Note"
      puts "2. View Notes"
      puts "3. Exit"
      print "Choose option: "
      choice = gets.chomp.to_i
    
      case choice
      when 1
    
    print "Enter your note: "
    note = gets.chomp
    File.open(FILENAME, "a") { |f| f.puts(note) }
    puts "Note saved!"
    when 2
    if File.exist?(FILENAME)
      puts "\nYour Notes:"
      puts File.read(FILENAME)
    else
      puts "No notes found."
    end
    when 3
    puts "Goodbye!"
    break
    else
    puts "Invalid option."
    end end

    Explanation:

    • "a" → appends to file without deleting old notes.
    • File.read → reads all notes.
    • Stores notes in notes.txt.
  • Student Grade Manager

    Concepts Used: Hashes, Loops, Conditionals

    students = {}
    
    loop do
      puts "\n--- Student Grade Manager ---"
      puts "1. Add Student"
      puts "2. View Students"
      puts "3. Exit"
      print "Choose option: "
    
      choice = gets.chomp.to_i
    
      case choice
      when 1
    
    print "Enter student name: "
    name = gets.chomp
    print "Enter grade: "
    grade = gets.chomp
    students&#91;name] = grade
    puts "Student added!"
    when 2
    puts "\nStudents List:"
    students.each { |name, grade| puts "#{name} - #{grade}" }
    when 3
    puts "Goodbye!"
    break
    else
    puts "Invalid choice!"
    end end

    Explanation:

    • Uses a hash → { "Ali" => "A", "Sara" => "B" }.
    • Allows adding and viewing students.
  • Simple Calculator

    Concepts Used: Methods, Conditionals, Loops

    def calculator
      puts "Enter first number:"
      a = gets.chomp.to_f
    
      puts "Enter second number:"
      b = gets.chomp.to_f
    
      puts "Choose operation (+, -, *, /):"
      op = gets.chomp
    
      result = case op
    
           when "+" then a + b
           when "-" then a - b
           when "*" then a * b
           when "/" then b != 0 ? a / b : "Error: Division by zero"
           else "Invalid operation"
           end
    puts "Result: #{result}" end calculator

    Explanation:

    • gets.chomp → reads user input.
    • case ... when → chooses operation.
    • Handles division by zero.
  • To-Do List

    Concepts Used: Arrays, Loops, Methods, File Handling

    tasks = []
    
    loop do
      puts "\n--- To-Do List ---"
      puts "1. Add Task"
      puts "2. View Tasks"
      puts "3. Remove Task"
      puts "4. Exit"
      print "Choose option: "
    
      choice = gets.chomp.to_i
    
      case choice
      when 1
    
    print "Enter task: "
    task = gets.chomp
    tasks &lt;&lt; task
    puts "Task added!"
    when 2
    puts "\nYour Tasks:"
    tasks.each_with_index { |t, i| puts "#{i+1}. #{t}" }
    when 3
    print "Enter task number to remove: "
    num = gets.chomp.to_i
    if num &gt; 0 &amp;&amp; num &lt;= tasks.length
      tasks.delete_at(num-1)
      puts "Task removed!"
    else
      puts "Invalid task number."
    end
    when 4
    puts "Goodbye!"
    break
    else
    puts "Invalid choice!"
    end end

    Explanation:

    • Stores tasks in an array.
    • Uses loop do ... end for continuous menu.
    • each_with_index → shows tasks with numbers.
    • delete_at → removes task by index.