eng
competition

Text Practice Mode

Ruby practice

created Tuesday July 22, 15:10 by Zaphod Beeblebrox


0


Rating

452 words
12 completed
00:00
# Basic data types and variables
name = "Alice"
age = 30
is_student = true
pi_value = 3.14159
numbers = [1, 2, 3, 4, 5]
user_data = { "name" => "Bob", "age" => 25, :city => "New York" }
puts "Hello, #{name}! You are #{age} years old."
 
# Control flow: if/else, unless, case
if age >= 18
  puts "You are an adult."
elsif age >= 13
  puts "You are a teenager."
else
  puts "You are a child."
end
 
unless is_student
  puts "This person is not currently a student."
end
 
grade = 'B'
case grade
when 'A'
  puts "Excellent!"
when 'B'
  puts "Good job!"
when 'C'
  puts "Pass."
else
  puts "Needs improvement."
end
 
# Loops: while, until, for, each
count = 0
while count < 3
  puts "While loop iteration: #{count}"
  count += 1
end
 
num = 5
until num == 0
  puts "Until loop iteration: #{num}"
  num -= 1
end
 
for number in numbers
  puts "For loop number: #{number}"
end
 
numbers.each do |n|
  puts "Each loop number: #{n}"
end
 
# Methods and blocks
def greet(person_name)
  puts "Greetings, #{person_name}!"
end
 
greet("Charlie")
 
def calculate_sum(a, b)
  a + b # Implicit return
end
 
result = calculate_sum(10, 20)
puts "Sum: #{result}"
 
# Blocks with parameters
[10, 20, 30].map do |x|
  x * 2
end.each { |doubled| puts "Doubled: #{doubled}" }
 
# Classes and Objects
class Person
  attr_accessor :name, :age # Creates getter and setter methods
 
  def initialize(name, age)
    @name = name
    @age = age
  end
 
  def introduce
    "Hi, my name is #{@name} and I am #{@age} years old."
  end
end
 
my_person = Person.new("David", 40)
puts my_person.introduce
my_person.age = 41
puts "David's new age: #{my_person.age}"
 
# Inheritance
class Student < Person
  attr_accessor :student_id
 
  def initialize(name, age, student_id)
    super(name, age) # Calls the parent class's initialize method
    @student_id = student_id
  end
 
  def introduce
    "#{super} My student ID is #{@student_id}."
  end
end
 
my_student = Student.new("Eve", 22, "S12345")
puts my_student.introduce
 
# Modules
module MathOperations
  def self.add(x, y)
    x + y
  end
 
  def self.subtract(x, y)
    x - y
  end
end
 
puts "Module addition: #{MathOperations.add(5, 3)}"
 
# Error handling
begin
  # This might raise an error
  10 / 0
rescue ZeroDivisionError => e
  puts "Caught an error: #{e.message}"
ensure
  puts "This block always executes."
end
 
# Symbols, Strings, and Arrays
symbol_example = :my_symbol
string_example = "This is a string."
array_example = [1, "two", :three, true]
puts "Symbol: #{symbol_example}, String length: #{string_example.length}"
puts "Array first element: #{array_example[0]}, last: #{array_example.last}"
 
# Hashes
hash_example = { key1: "value1", key2: "value2" }
puts "Hash value for key1: #{hash_example[:key1]}"
hash_example[:key3] = 123
puts "All hash keys: #{hash_example.keys}"
 
# Regular expressions
if "hello world" =~ /world/
  puts "Pattern found!"
end
 
# File operations (brief example)
File.open("example.txt", "w") do |file|
   file.puts "Hello from Ruby!"
end
puts "File 'example.txt' created."

saving score / loading statistics ...