WmZilla - Webmaster and Marketplace

The Next Generation Webmaster and Trade Forum

Guide Ruby #2 - General Features

foz

New member

0

0%

Status

Offline

Posts

35

Likes

0

Rep

0

Bits

185

3

Months of Service

0%
In my previous topic, Ruby #1 - Introduction, I provided a brief overview of what Ruby is, its history, and how you can install it on your systems. In this guide, I aim to delve into more detailed information about Ruby. The topics covered in this guide include the general features of Ruby, its syntax, and its working principles.

General Features of Ruby
Ruby, as described by its creator Matz, is a programming language that is based more on naturalness than simplicity. For this reason, Matz took features from languages he loved (Perl, Smalltalk, Eiffel, Ada, and Lisp) and blended them to create Ruby, making it a language that serves a general purpose rather than focusing on a specific domain. As a result of its features, Ruby has not faded away over time and is still being used today.

Object-Oriented Approach
Contrary to many object-oriented approach philosophies adopted by other languages, Ruby is more engaged with this philosophy than other languages. This is because Matz wanted Ruby to be more object-oriented compared to Python. In Ruby, everything you see is an object, including numbers, arrays, and classes. This naturally makes Ruby more flexible than it should be.

Flexibility
In addition to everything being an object in Ruby, because Ruby dynamically checks data types, there is no need for you to write extra code for type checking. Additionally, Ruby does not impose how something should be done. You are completely free as long as you know what you are doing. For example, consider the following code snippet:

```ruby
class Numeric
def plus(x)
self.+(x)
end
end

y = 5.plus 6
```

As you can see, in Ruby, you can easily create an alternative to using the + operator by creating a method called plus to perform the same function. Although this is a simple example, it gives you some insight into Ruby's flexibility. I will delve further into Ruby's advanced features in future guides, but for now, this information should suffice.

Simplified Syntax
Although the syntax of most high-level languages is easy, Ruby may have one of the cleanest and most readable syntaxes of any programming language. When you look at Ruby code, you may feel like you are reading English text rather than code. However, it is worth noting that you can also easily make Ruby code complex. Let's examine two different code examples. One will be written using an average technique, and the other will be written using Ruby's features. The application we will create is very simple; we will check each number in an array one by one and display the output if it is even:

```ruby
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

numbers.each do |number|
if number % 2 == 0
puts number
end
end
```

Normally, we can easily iterate over numbers and determine whether they are even with a simple algorithm. While the length is not critical, it is possible to shorten this code by taking advantage of Ruby's features.

```ruby
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

numbers.select(&:even?).each(&method:)puts))
```

As you can see, by using Ruby's features, we can reduce the operation to one line. The part .select(&:even?) reads each number and uses Ruby's .even? check method to determine if the number is even. The part .each(&method:)puts)) then sends each even number as an output to the command line using the puts method. The main difference is that while you would normally have to write the algorithm for the check yourself, Ruby does it for you. By being able to chain methods (which is present in almost every language), you can reduce the code to a single line. Let's make our job even easier without dealing with variables:

```ruby
(1..10).each { |n| puts n if n.even? }
```

Again, by using Ruby's unique features, we can further simplify the previous code without compromising the outcome.

In general, these are the topics I can cover for now. In future guides, I will discuss Ruby's unique features and demonstrate how you can use them through examples. Until the next guide, best regards.
 

249

6,622

6,642

Top