← Ruby EnglishChapter 01 of 13

Introduction to Ruby

## Learning Objectives - Understand Ruby history and philosophy - Set up Ruby development environment - Write and run your first Ruby program - Understand Ruby syntax basics ## What is Ruby? Ruby is a dynamic, object-oriented programming language created by Yukihiro Matsumoto (Matz) in 1995. ```ruby puts "Hello, World!" ``` ## Ruby Philosophy ### Design Principles - **Programmer Happiness** - Ruby is designed to make developers happy - **Convention over Configuration** - Sensible defaults reduce decision fatigue - **DRY** - Don't Repeat Yourself - **TIMTOWTDI** - There Is More Than One Way To Do It ### Key Features - Pure object-oriented (everything is an object) - Dynamic typing - Garbage collection - Interactive console (IRB) - Rich standard library ## Installing Ruby ### Download Get Ruby from or use a version manager: ```bash # Using rbenv (macOS/Linux) brew install rbenv rbenv init # Using RubyInstaller (Windows) # Download from https://rubyinstaller.org/ ``` ### Verify Installation ```bash ruby --version ``` ## Your First Program ### File: hello_world.rb ```ruby puts "Hello, World!" ``` ### Run the Program ```bash ruby hello_world.rb ``` ## Ruby Program Structure ```ruby # This is a comment # Define a method def greet(name) "Hello, #{name}!" end # Call the method and print result puts greet("World") ``` ### Components 1. **Comments**: Lines starting with `#` 2. **Methods**: Defined with `def...end` 3. **Variables**: No type declaration needed 4. **String interpolation**: `"#{expression}"` ## Comments ```ruby # Single-line comment =begin Multi-line comment This is rarely used in practice =end ``` ## The puts Method ```ruby # Print with newline puts "Hello" puts "World" # Print without newline print "Hello " print "World" puts # Print array (each element on new line) puts [1, 2, 3] # Output: # 1 # 2 # 3 ``` ## Print vs puts vs p ```ruby name = "Alice" print "Hello: " # No newline, no quotes puts "Hello: " # Newline, no quotes p "Hello: " # Newline, with quotes (debugging) puts name # Alice p name # "Alice" (shows quotes - useful for debugging) ``` ## Interactive Ruby (IRB) ```bash irb ``` ```ruby irb(main):001:0> 1 + 2 => 3 irb(main):002:0> "hello".upcase => "HELLO" irb(main):003:0> exit ``` ## Variables ```ruby name = "Ruby" # Local variable @age = 25 # Instance variable @@count = 0 # Class variable PI = 3.14159 # Constant ``` ## Naming Conventions | Type | Convention | Example | |------|------------|---------| | Local variable | lowercase with underscore | `first_name` | | Instance variable | @ prefix | `@name` | | Class variable | @@ prefix | `@@counter` | | Constant | UPPERCASE | `MAX_SIZE` | | Method name | lowercase with underscore | `def calculate_total` | ## Command Line Arguments ```ruby # hello.rb name = ARGV[0] || "World" puts "Hello, #{name}!" ``` ```bash ruby hello.rb Alice # Output: Hello, Alice! ``` ## Ruby Versions ```bash ruby --version ``` | Version | Year | Feature | |---------|------|---------| | Ruby 2.5 | 2017 | Argument forwarding | | Ruby 2.7 | 2019 | Pattern matching | | Ruby 3.0 | 2020 | Ractor, RBS | | Ruby 3.2 | 2022 | YJIT compiler | ## IDEs ### Popular Editors - Visual Studio Code (recommended) - RubyMine - Sublime Text - Vim/Neovim with vim-ruby ### Useful VS Code Extensions - Ruby - erb - Ruby Solargraph ## Summary - Ruby is designed for programmer happiness - Everything in Ruby is an object - Use `ruby` command to run `.rb` files - Use `puts` for output, `print` for inline - IRB provides interactive Ruby exploration - Variable types are inferred (no declaration needed)

Comments

Comments powered by Giscus

To enable comments, add your Giscus embed code here.

Learn more about Giscus →