Ruby Basics and Syntax
Ruby is a human-centric, highly readable, object-oriented language designed for "programmer happiness."
1. Philosophy of Ruby
- Principle of Least Astonishment (POLA): The language is designed so that its behavior is always predictable and intuitive for the programmer.
- Everything is an Object: Numbers, strings, and even
nilare all objects in Ruby. - Elegant Syntax: Aims for natural-sounding code that reads like English sentences.
2. Basic Syntax
name = "Ruby" # Variable declaration
puts "Hello, #{name}!" # String interpolation
3.times { puts "Helpful repeated output" } # Using Blocks
3. Flexible Method Invocation
In many cases, parentheses can be omitted, leading to very concise and clean code.
def say_hello(target)
"Hello, " + target
end
puts say_hello "World" # Parentheses are optional
4. Gems (Package Management)
In the Ruby ecosystem, libraries are called Gems. Developers use a tool called Bundler to manage project-specific dependencies.
In the next section, we will learn about Ruby's "killer app," Ruby on Rails.