CodeToLive

Introduction to Ruby

Ruby is a dynamic, object-oriented programming language designed for simplicity and productivity. It has an elegant syntax that is natural to read and easy to write. Ruby is widely used for web development, scripting, and automation.

Key Features of Ruby:

Installing Ruby

To get started with Ruby, you need to install it on your computer. You can download Ruby from the official website: Ruby Downloads.

Interactive Ruby (IRB)

Ruby comes with an interactive shell called IRB (Interactive Ruby). You can use it to experiment with Ruby code.


$ irb
irb(main):001:0> puts "Hello, IRB!"
Hello, IRB!
=> nil
      

Basic Syntax

Ruby's syntax is straightforward and easy to learn. Here are some basic examples:


# Variables
name = "Alice"
age = 25

# Strings
greeting = "Hello, #{name}!"

# Arrays
fruits = ["apple", "banana", "cherry"]

# Hashes
person = { name: "Alice", age: 25 }

# Control Structures
if age > 18
  puts "You are an adult."
else
  puts "You are a minor."
end
      

Variables and Data Types

Ruby supports various data types, including strings, numbers, arrays, and hashes.


# Strings
name = "Alice"

# Numbers
age = 25
pi = 3.14159

# Arrays
fruits = ["apple", "banana", "cherry"]

# Hashes
person = { name: "Alice", age: 25 }
      

Best Practices

Next: Variables and Data Types