Loading Interactive Course...
Ruby is a dynamic, open-source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write. In this module, you'll learn the basic structure of Ruby programs and how to write your first Ruby code.
puts is used to output text to the console# and are ignored by Ruby+, -, *, /#{expression} works only with double quotes== for equality comparisontrue and false (all lowercase)Ruby is a dynamically typed language, meaning you don't have to declare variable types. It has several built-in data types including numbers, strings, booleans, arrays, and hashes. Understanding these fundamental types is crucial for effective Ruby programming.
my_variable42 (integer) vs 3.14 (float)array[0] gets first elementhash["key"] = "value":symbol_name (more memory efficient than strings).class to check an object's typeControl structures allow your program to make decisions and repeat actions. Ruby provides conditional statements like if, unless, case and loops like while, until, and for to control the flow of your program.
if executes code when condition is trueunless executes code when condition is false (opposite of if)case statements are cleaner than multiple if-elsif statementswhile loops continue while condition is trueuntil loops continue until condition becomes true1..5 includes 5, 1...5 excludes 5each is the preferred way to iterate in Ruby (more idiomatic than for)times is useful for repeating a block a specific number of timesaction if conditionend to close blocks, not curly braces like other languagesMethods in Ruby are reusable blocks of code that perform specific tasks. They help organize your code, make it more readable, and reduce repetition. Ruby methods can take parameters, return values, and even have default arguments.
def method_name and end with endmy_method_namedef greet(name="Guest")return statements can be used for early returns? and return boolean values! and modify the object they're called on*args to accept any number of parametersgreet "Alice" vs greet("Alice")Arrays and hashes are fundamental data structures in Ruby. Arrays are ordered collections, while hashes are key-value pairs. Ruby provides powerful methods to manipulate these collections efficiently.
{name: "Alice"} is shorthand for {:name => "Alice"}push/<< to add, pop to remove last, shift/unshift for first elementeach for iteration, map for transformation, select for filteringeach_with_index provides both element and index during iterationhash[:key]sort, reverse, empty?, include?join converts array to string with specified separatorRuby is a pure object-oriented language where everything is an object. This module covers classes, objects, inheritance, and other OOP concepts that make Ruby powerful and flexible for building complex applications.
class ClassName (capitalized, CamelCase)@ and are available throughout the objectinitialize method is the constructor called when creating new objectsattr_accessor automatically creates getter and setter methodsattr_reader creates getters only, attr_writer creates setters onlyclass Child < Parent syntaxsuper calls the parent class's method of the same namedef self.method_nameincludeUse this space to experiment with everything you've learned. Try writing your own Ruby programs and see the results in real-time! This is your sandbox to practice and explore Ruby programming.