No history yet

Ruby Basics

Meet Ruby

Ruby is a programming language designed for simplicity and productivity. Its syntax is clean, readable, and often feels like writing in plain English. This makes it a great choice for beginners.

One of the first things you'll notice is that you don't need to end lines with a semicolon. Ruby understands that a new line means a new command. You can focus on the logic without worrying about extra punctuation.

To leave notes for yourself or other programmers, you can write comments. In Ruby, any text following a hash symbol (#) on a line is a comment and will be ignored by the computer.

# This is a comment. Ruby will not run this line.

puts "Hello, world!" # This part is code, this is a comment.

Storing Information

To work with data, we need a place to store it. In programming, we use variables for this. Think of a variable as a labeled box where you can keep information. You give it a name and put a value inside.

message = "Welcome to Ruby!"
user_age = 25

In the example above, message and user_age are variable names. Ruby is dynamically typed, which means you don't have to tell it what kind of data a variable will hold. Ruby figures it out on its own. The data itself comes in a few common forms, or types.

Numbers: These can be whole numbers like 10 (integers) or numbers with decimal points like 3.14 (floats). Strings: This is how we store text. A string is just a sequence of characters wrapped in quotes, like "Hello" or 'Ruby'. Booleans: A boolean represents a truth value. It can only be one of two things: true or false. Nil: This special value represents the absence of value, or "nothing". If a variable doesn't have anything in it, its value is nil.

Double quotes ("") are special for strings. They allow for string interpolation, which is a fancy way of saying you can embed expressions directly into a string.

name = "Ada"
puts "Hello, #{name}!" # Prints "Hello, Ada!"

Doing the Work

Once we have data, we can perform operations on it. Operators are the symbols that do the work, like + for addition or * for multiplication.

OperatorNameExampleResult
+, -, *, /Arithmetic10 / 25
%Modulo10 % 31
**Exponent2 ** 38
==Equal5 == 5true
!=Not equal"a" != "b"true
>Greater than10 > 5true

To make programs interactive, we need to get input from a user and display output. The puts command prints a value to the screen and adds a new line. The print command does the same without the new line. To get input, we use gets.

print "What's your name? "

# gets reads user input, and chomp removes the extra line break
name = gets.chomp

puts "Nice to meet you, #{name}!"

Organizing Data

Sometimes you need to store more than just one piece of data. For lists of items, Ruby gives us arrays. For storing connected pairs of information, we use hashes.

array

noun

An ordered list of values, like a numbered shopping list. Arrays are created using square brackets [].

Array elements are indexed starting from zero. You access an element by putting its index in square brackets.

shopping_list = ["apples", "bread", "milk"]

puts shopping_list[0]  # Prints "apples"
puts shopping_list[2]  # Prints "milk"

A hash is a collection of key-value pairs, like a dictionary. Each value is associated with a unique key. You create hashes with curly braces {}.

# Using symbols as keys (the modern way)
person = {name: "Alice", age: 30}

# Accessing a value using its key
puts person[:name] # Prints "Alice"

Hashes are perfect for when the order of items doesn't matter, but you need to look up values by a specific identifier, like finding a person's age by using their name as the key.

Now, let's test your understanding of these fundamental concepts.

Quiz Questions 1/6

How do you write a single-line comment in Ruby?

Quiz Questions 2/6

In Ruby, you must declare the data type of a variable before assigning a value to it.

You now have the basic building blocks of Ruby: a way to store data, operate on it, and organize it into simple collections. These concepts are the foundation for writing more complex and powerful programs.