Ruby is a dynamic, open-source programming language with a focus on simplicity and productivity. It has a elegant syntax that is natural to read and easy to write. Whether you're new to programming or looking to expand your skills, Ruby is a great choice.
Installation
To start using Ruby, you need to install it on your computer. You can download and install Ruby from the official Ruby website.
Hello World
Your first Ruby program can be as simple as:
puts "Hello, World!"
This program prints "Hello, World!" to the console.
Variables
In Ruby, variables are used to store data values. Variables must be given a name and a value. The name of a variable can start with a letter or an underscore, followed by any number of letters, numbers, or underscores.
name = "Alice"
age = 25
Control Structures
Ruby uses if/else statements for conditional logic.
if age > 18
puts "You are an adult."
else
puts "You are not an adult."
end
Loops
Ruby has several loop constructs, such as while and for.
i = 0
while i < 5
puts i
i += 1
end
Arrays
Arrays are used to store collections of data.
fruits = ["apple", "banana", "cherry"]
puts fruits[0] # Output: apple
Methods
Methods are blocks of code that perform a specific task.
def greet(name)
puts "Hello, #{name}!"
end
greet("Alice") # Output: Hello, Alice!
Ruby on Rails
Ruby on Rails is a popular web application framework that uses Ruby. It simplifies the process of building web applications.
# Create a new Rails application
rails new myapp
Learn More
To learn more about Ruby, check out the following resources: