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
- Comments
- Variables
- Data Types
- Operators
- Control Structures
- Methods
- Comments
Comments are used to explain code and are ignored by the Ruby interpreter.
- Single-line comments start with
#
.
- Multi-line comments are enclosed between
=begin
and=end
.
- 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.
- Instance Variables: Start with an
@
.
- Class Variables: Start with
@@
.
- Global Variables: Start with
$
.
- Data Types
Ruby supports several data types, including:
- Numbers: Integers and floating-point numbers.
- Strings: Text enclosed in quotes.
- Symbols: Immutable, unique identifiers.
- Arrays: Ordered collections of objects.
- Hashes: Collections of key-value pairs.
- Operators
Ruby supports various operators for arithmetic, comparison, and logical operations.
- Arithmetic Operators:
+
,-
,*
,/
,%
- Comparison Operators:
==
,!=
,>
,<
,>=
,<=
- Logical Operators:
&&
,||
,!
- 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
- Methods
Methods are blocks of code that perform a specific task.
- Defining a Method: Use the
def
keyword.
- Returning Values: Use the
return
keyword (optional, as Ruby returns the last evaluated expression by default).
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.
Ruby Programming Course
Module 1: Introduction to Ruby
Module 2: Basic Ruby Concepts
Module 3: Working with Collections
Module 4: Object-Oriented Programming in Ruby
- Classes and Objects
- Instance Variables and Methods
- Class Variables and Methods
- Inheritance
- Modules and Mixins
Module 5: Advanced Ruby Concepts
Module 6: Ruby on Rails Introduction
- What is Ruby on Rails?
- Setting Up Rails Environment
- Creating a Simple Rails Application
- MVC Architecture
- Routing
Module 7: Testing in Ruby
- Introduction to Testing
- Unit Testing with Minitest
- Behavior-Driven Development with RSpec
- Mocking and Stubbing