Introduction to Ruby — Ruby Deep Dive [01]
Ruby code to quickly get going with the language: RubyPrograms by Bhavyansh on GitHub
Ruby is a dynamic, open-source programming language with a focus on simplicity and productivity. Created by Yukihiro “Matz” Matsumoto in the mid-1990s, Ruby has since grown to be a favorite among developers for its elegant syntax and powerful features.
Let’s dive into some of the core aspects that make Ruby a unique and practical choice for developers.
Object-Oriented Nature
Ruby is a pure object-oriented language. This means everything in Ruby, including numbers and other primitive data types, are objects. This design makes Ruby highly intuitive and flexible.
Practical Example: Creating and Using Classes
class Animal
def initialize(name)
@name = name
end
def speak
"Hello, my name is #{@name}."
end
end
dog = Animal.new("Buddy")
puts dog.speak # Output: Hello, my name is Buddy.
In this example, we define an Animal
class with an initializer and a speak
method. We then create an instance of Animal
and call the speak
method, demonstrating Ruby's object-oriented capabilities.
Everything in Ruby is an Object
# Checking the class of an integer
puts 42.class # Output: Integer
# Checking the class of the class Integer
puts 42.class.class # Output: Class
# Checking the class of the class Class
puts 42.class.class.class # Output: Class
# Checking the superclass of Integer
puts 42.class.superclass # Output: Numeric
# Checking the top-level parent class of Integer
puts 42.class.superclass.superclass.superclass # Output: BasicObject
Dynamic Features
Ruby’s dynamic nature allows developers to write flexible and adaptable code. It supports dynamic typing and duck typing, enabling you to write more general and reusable code.
Practical Example: Dynamic Typing
def add(a, b)
a + b
end
puts add(2, 3) # Output: 5
puts add("Hello, ", "World!") # Output: Hello, World!
Here, the add
method works with both integers and strings due to Ruby's dynamic typing. This flexibility can lead to more versatile and concise code.
Comparison with Other Languages
Ruby stands out from other languages like Python, JavaScript, and Java in various ways. Let’s explore these differences with practical examples.
Practical Comparison with Python
Ruby:
5.times { puts "Hello" }
Python:
for _ in range(5):
print("Hello")
Ruby’s block syntax (5.times {}
) is more concise compared to Python's loop.
Practical Comparison with JavaScript
Ruby:
[1, 2, 3].map { |n| n * 2 } # Output: [2, 4, 6]
JavaScript:
[1, 2, 3].map(n => n * 2); // Output: [2, 4, 6]
Ruby’s array manipulation is clean and straightforward, similar to JavaScript but with less boilerplate.
Practical Comparison with Java
Ruby:
class Greeting
def say_hello
"Hello, World!"
end
end
greet = Greeting.new
puts greet.say_hello # Output: Hello, World!
Java:
public class Greeting {
public String sayHello() {
return "Hello, World!";
}
public static void main(String[] args) {
Greeting greet = new Greeting();
System.out.println(greet.sayHello());
}
}
Ruby’s code is much more concise and readable compared to Java’s verbose syntax.
Foundational Elements
Ruby was designed to make programming both fun and productive. It’s built on several key principles:
- Simplicity and Productivity: Ruby’s syntax is designed to be natural and easy to write.
- Convention over Configuration: Popularized by the Ruby on Rails framework, this principle helps developers by providing sensible defaults and reducing the need for extensive configuration.
- Open-Source and Community-Driven: Ruby is open-source and has a vibrant community contributing to its development and the creation of gems (libraries).
Ruby Conventions and Idioms
Ruby’s conventions and idioms help in writing cleaner, more readable code.
- Spacing: Use two spaces instead of tabs for indentation.
- Naming Conventions: Use snake_case for variables and methods, CamelCase for classes and modules, and SCREAMING_SNAKE_CASE for constants.
- Idiomatic Code: Write code that is idiomatic to Ruby, leveraging its elegant syntax and powerful features.