Ruby - Introduction

# Fundamentals
Easy

Ruby: Introduction

Ruby is a dynamic, interpreted, and object-oriented programming language created by Yukihiro Matsumoto (also known as Matz) in Japan in the mid-1990s. It was designed to be simple, flexible, and productive, with an elegant and expressive syntax that promotes readability and developer productivity.
Ruby is frequently associated with web development, especially due to the popular Ruby on Rails framework, but it's also used in various other areas including task automation, scripting, game development, and more.

Key Features of Ruby

  1. Object-Oriented Programming:
  2. Everything in Ruby is an object, including numbers, strings, and even classes and methods.
  3. It follows the object-oriented programming paradigm, with features such as inheritance, polymorphism, and encapsulation.
  4. Clean and Expressive Syntax:
  5. Ruby has a simple and readable syntax, inspired by languages like Perl and Smalltalk.
  6. Its code is often described as "beautiful" and "elegant".
  7. Metaprogramming:
  8. Ruby is known for its powerful metaprogramming capabilities, allowing programs to modify and adapt themselves at runtime.
  9. Blocks and Iterators:
  10. Ruby supports blocks and iterators, enabling the creation of concise and expressive code, especially for operations on data collections.
  11. Functional Programming:
  12. Although it's an object-oriented language, Ruby also supports functional programming concepts, such as first-class functions and higher-order methods.
  13. Active Community and Gem Ecosystem:
  14. Ruby has an active and vibrant community, with a wide variety of libraries and frameworks available for a broad range of applications.

Code Example

hello.rb
1# Example of a class in Ruby
2class Person
3 attr_accessor :name
4
5 def initialize(name)
6 @name = name
7 end
8
9 def greeting
10 "Hello, my name is #{@name}."
11 end
12end
13
14# Creating an instance of the Person class
15p1 = Person.new("John")
16
17# Calling the greeting method
18puts p1.greeting # Output: Hello, my name is John.
In this example, we create a Person class with a greeting method that returns a greeting with the person's name. Then, we instantiate the class and call the method to display the greeting.

Working with Primitive Types

In Ruby, you can discover the type of any object by using the .class method:
types.rb
142.class # Returns Integer
2"hello".class # Returns String
33.14.class # Returns Float
4true.class # Returns TrueClass
It's also possible to find out the ancestors (inheritance chain) of any class by using the .ancestors method:
ancestors.rb
1String.ancestors # Returns [String, Comparable, Object, Kernel, BasicObject]
2Integer.ancestors # Returns [Integer, Numeric, Comparable, Object, Kernel, BasicObject]
For more detailed information, refer to the official Ruby documentation which provides comprehensive guides and API references.

Ruby Playground
Ruby
Loading...
Console
Hello Ruby-Maniac... :)