In this section, we will cover the fundamental syntax and structure of Ruby. Understanding these basics is crucial as they form the foundation for writing and understanding Ruby code.

Key Concepts

  1. Comments
  2. Variables
  3. Data Types
  4. Operators
  5. Control Structures
  6. Methods

  1. Comments

Comments are used to explain code and are ignored by the Ruby interpreter.

  • Single-line comments start with #.
# This is a single-line comment
puts "Hello, World!" # This comment is after a code statement
  • Multi-line comments are enclosed between =begin and =end.
=begin
This is a
multi-line comment
=end
puts "Hello, World!"

  1. Variables

Variables are used to store data that can be referenced and manipulated in a program.

  • Local Variables: Start with a lowercase letter or an underscore.
name = "Alice"
age = 30
  • Instance Variables: Start with an @.
@name = "Alice"
  • Class Variables: Start with @@.
@@total_users = 100
  • Global Variables: Start with $.
$global_var = "I am global"

  1. Data Types

Ruby supports several data types, including:

  • Numbers: Integers and floating-point numbers.
integer = 42
float = 3.14
  • Strings: Text enclosed in quotes.
single_quoted = 'Hello'
double_quoted = "World"
  • Symbols: Immutable, unique identifiers.
symbol = :my_symbol
  • Arrays: Ordered collections of objects.
array = [1, 2, 3, "four"]
  • Hashes: Collections of key-value pairs.
hash = { "name" => "Alice", "age" => 30 }

  1. Operators

Ruby supports various operators for arithmetic, comparison, and logical operations.

  • Arithmetic Operators: +, -, *, /, %
sum = 5 + 3
difference = 5 - 3
product = 5 * 3
quotient = 5 / 3
remainder = 5 % 3
  • Comparison Operators: ==, !=, >, <, >=, <=
is_equal = (5 == 5) # true
is_not_equal = (5 != 3) # true
is_greater = (5 > 3) # true
  • Logical Operators: &&, ||, !
and_result = (true && false) # false
or_result = (true || false) # true
not_result = !true # false

  1. Control Structures

Control structures allow you to control the flow of your program.

  • Conditional Statements: if, elsif, else
if age < 18
  puts "You are a minor."
elsif age >= 18 && age < 65
  puts "You are an adult."
else
  puts "You are a senior."
end
  • Case Statements: Similar to switch statements in other languages.
grade = 'A'

case grade
when 'A'
  puts "Excellent!"
when 'B'
  puts "Good job!"
when 'C'
  puts "Well done!"
else
  puts "Keep trying!"
end

  1. Methods

Methods are blocks of code that perform a specific task.

  • Defining a Method: Use the def keyword.
def greet(name)
  puts "Hello, #{name}!"
end

greet("Alice") # Output: Hello, Alice!
  • Returning Values: Use the return keyword (optional, as Ruby returns the last evaluated expression by default).
def add(a, b)
  return a + b
end

sum = add(5, 3) # sum is 8

Practical Exercises

Exercise 1: Basic Arithmetic

Write a Ruby program that takes two numbers as input and performs addition, subtraction, multiplication, and division.

# Solution
puts "Enter the first number:"
num1 = gets.chomp.to_i

puts "Enter the second number:"
num2 = gets.chomp.to_i

puts "Addition: #{num1 + num2}"
puts "Subtraction: #{num1 - num2}"
puts "Multiplication: #{num1 * num2}"
puts "Division: #{num1 / num2}"

Exercise 2: Conditional Statements

Write a Ruby program that takes a user's age as input and prints whether they are a minor, an adult, or a senior.

# Solution
puts "Enter your age:"
age = gets.chomp.to_i

if age < 18
  puts "You are a minor."
elsif age >= 18 && age < 65
  puts "You are an adult."
else
  puts "You are a senior."
end

Exercise 3: Methods

Write a Ruby method that takes a string as input and returns the string reversed.

# Solution
def reverse_string(str)
  return str.reverse
end

puts "Enter a string:"
input = gets.chomp
puts "Reversed string: #{reverse_string(input)}"

Summary

In this section, we covered the basic syntax and structure of Ruby, including comments, variables, data types, operators, control structures, and methods. These fundamentals are essential for writing and understanding Ruby code. In the next module, we will delve deeper into basic Ruby concepts such as variables, data types, and operators.

© Copyright 2024. All rights reserved