Ruby on Rails Web Development Fundamentals
Introduction to Ruby
The Ruby Language
Ruby is a programming language designed with a focus on simplicity and productivity. Its creator, Yukihiro "Matz" Matsumoto, blended parts of his favorite languages to form a new language that balances functional programming with imperative programming. The goal was to make programming feel natural and enjoyable for developers.
Often, people who are new to Ruby are surprised by its elegant and readable syntax. It almost looks like plain English.
Syntax and Data
Let's start with the building blocks. In Ruby, you store information in variables. Think of a variable as a labeled box where you can put a piece of data. Assigning a value is straightforward.
name = "Alice"
age = 30
pi = 3.14
is_learning = true
Ruby is dynamically typed, which means you don't have to declare a variable's type beforehand. The type is determined by the value you assign to it. Here are the most common data types you'll encounter:
| Data Type | Description | Example |
|---|---|---|
| String | A sequence of characters | "Hello, Ruby!" |
| Integer | A whole number | 100 or -50 |
| Float | A number with a decimal | 98.6 or 0.001 |
| Boolean | Represents true or false | true or false |
| Array | An ordered list of items | [1, "apple", true] |
| Hash | A collection of key-value pairs | {"name" => "Bob", "age" => 42} |
| Nil | Represents the absence of value | nil |
Actions in Ruby are performed by methods. A method is a reusable block of code that performs a specific task. You define a method using the def keyword and call it by its name.
# Defining a method
def say_hello(name)
# The #{} syntax is string interpolation
"Hello, #{name}!"
end
# Calling the method
puts say_hello("Developer")
# Output: Hello, Developer!
Making Decisions
Programs often need to make choices. Ruby's control structures let you direct the flow of your code based on certain conditions. The most common way to do this is with an if/else statement.
temperature = 75
if temperature > 80
puts "It's hot outside!"
elsif temperature > 60
puts "It's a pleasant day."
else
puts "It's a bit chilly."
end
# Output: It's a pleasant day.
When you need to perform an action repeatedly, you use a loop. While there are several ways to loop in Ruby, one of the most common is the each method, which iterates over items in a collection like an array.
fruits = ["apple", "banana", "cherry"]
fruits.each do |fruit|
puts "I love #{fruit}s."
end
# Output:
# I love apples.
# I love bananas.
# I love cherrys.
Everything is an Object
A core principle of Ruby is that everything is an object. This means every piece of data, from a number to a string, has built-in methods you can use. This makes the language consistent and powerful. For example, you can call the length method directly on a string to find out how long it is.
puts "Hello".length # Output: 5
puts 10.next # Output: 11
To create your own objects, you define a class. A class is a blueprint for creating objects with specific properties (called instance variables) and actions (methods).
class Dog
# The initialize method runs when a new object is created
def initialize(name)
@name = name # @name is an instance variable
end
def bark
"#{@name} says Woof!"
end
end
# Create a new Dog object (an instance of the class)
my_dog = Dog.new("Fido")
# Call the bark method on the new object
puts my_dog.bark # Output: Fido says Woof!
Sometimes you have a collection of methods that you want to share across different classes. For this, Ruby uses modules. A module is a bundle of methods that can be mixed into a class to add functionality. This is a powerful feature for keeping your code organized and reusable.
Handling Errors
Things don't always go as planned. Your code might try to divide by zero or open a file that doesn't exist. Ruby provides a simple way to handle these exceptions using a begin/rescue block. You put the code that might fail inside the begin section and the code to run if an error occurs in the rescue section.
begin
# This code will cause an error
result = 10 / 0
puts result
rescue ZeroDivisionError => e
# This code runs if a ZeroDivisionError occurs
puts "Error: You can't divide by zero!"
puts "Specific error: #{e.message}"
end
# Output: Error: You can't divide by zero!
# Output: Specific error: divided by 0
Now that you have a sense of the fundamentals, you're ready to see how these concepts apply in a larger framework.
Time to check what you've learned.
What is a core principle of Ruby's design philosophy mentioned in the text?
How do you define a reusable block of code that performs a specific task in Ruby?
Understanding these core Ruby concepts is the first major step toward mastering Ruby on Rails.