The Ruby Programming Language
Ruby is an object-oriented scripting language originally developed by Yukihiro Matsumoto (also known as Matz). The main website of the Ruby programming language is ruby-lang.org.
Development began in February 1993 and the first alpha version of Ruby was released in December 1994. It was developed to be an alternative to scripting languages such as Perl and Python. Ruby borrows heavily from Perl and the class library is essentially an object-oriented reorganization of Perl's functionality.
Ruby also borrows from Lisp and Smalltalk. While Ruby does not borrow many features from Python, reading the code for Python helped Matz develop Ruby.
MacOS comes with Ruby already installed. Most Linux distributions either come with Ruby preinstalled or allow you to easily install Ruby from the distribution's repository of free software.
You can also download and install Ruby on Windows. The more technically adept can download the Ruby source code and compile it for most operating systems, including Unix, DOS, BeOS, OS/2, Windows, and Linux.
Ruby combines features from Perl, Smalltalk, Eiffel, Ada, Lisp, and Python.
Ruby goes to great lengths to be a purely object oriented language. Every value in Ruby is an object, even the most primitive things: strings, numbers and even true and false.
Every object has a class and every class has one superclass. At the root of the class hierarchy is the class BasicObject, from which all other classes, including Object, inherit.
# Everything, including a literal, is an object, so this works: -199.abs # => 199 'ice is nice'.length # => 11 'ruby is cool.'.index('u') # => 1 "Nice Day Isn't It?".downcase.split('').uniq.sort.join # => " '?acdeinsty"
Every class has a set of methods which can be called on objects of that class. Methods are always called on an object — there are no “class methods”, as there are in many other languages (though Ruby does a great job at faking them).
Every object has a set of instance variables which hold the state of the object. Instance variables are created and accessed from within methods called on the object. Instance variables are completely private to an object.
No other object can see them, not even other objects of the same class, or the class itself. All communication between Ruby objects happens through methods.
In addition to classes, Ruby has modules. A module has methods, just like a class, but it has no instances. Instead, a module can be included, or “mixed in,” to a class, which adds the methods of that module to the class. This is very much like inheritance but far more flexible because a class can include many different modules.
By building individual features into separate modules, functionality can be combined in elaborate ways and code easily reused. Mix-ins help keep Ruby code free of complicated and restrictive class hierarchies.
Ruby is a very dynamic programming language. Ruby programs aren’t compiled, in the way that C or Java programs are. All of the class, module and method definitions in a program are built by the code when it is run. A program can also modify its own definitions while it’s running.
Even the most primitive classes of the language like String and Integer can be opened up and extended. Rubyists call this monkey patching and it’s the kind of thing you can’t get away with in most other languages.
Variables in Ruby are dynamically typed, which means that any variable can hold any type of object. When you call a method on an object, Ruby looks up the method by name alone — it doesn't care about the type of the object.
This is called duck typing and it lets you make classes that can pretend to be other classes, just by implementing the same methods.
When I said that every Ruby object has a class, I lied. The truth is, every object has two classes: a “regular” class and a singleton class. An object’s singleton class is a nameless class whose only instance is that object. Every object has its very own singleton class, created automatically along with the object.
Singleton classes inherit from their object’s regular class and are initially empty, but you can open them up and add methods to them, which can then be called on the lone object belonging to them. This is Ruby’s secret trick to avoid “class methods” and keep its type system simple and elegant.
Ruby is so object oriented that even classes, modules and methods are themselves objects! Every class is an instance of the class Class and every module is an instance of the class Module. You can call their methods to learn about them or even modify them, while your program is running.
That means that you can use Ruby code to generate classes and modules, a technique known as metaprogramming. Used wisely, metaprogramming allows you to capture highly abstract design patterns in code and implement them as easily as calling a method.
In Ruby, everything is malleable. Methods can be added to existing classes without subclassing, operators can be overloaded, and even the behavior of the standard library can be redefined at runtime.
Variables and scope
You do not need to declare variables or variable scope in Ruby. The name of the variable automatically determines its scope.
x is a local variable (or something other than a variable).
- $x is a global variable.
- @x is an instance variable.
- @@x is a class variable.
Blocks are one of Ruby’s most unique and most loved features. A block is a piece of code that can appear after a call to a method, like this:
laundry_list.sort do |a,b| a.color <=> b.color end
The block is everything between the do and the end. The code in the block is not evaluated right away, rather it is packaged into an object and passed to the sort method as an argument. That object can be called at any time, just like calling a method.
The sort method calls the block whenever it needs to compare two values in the list. The block gives you a lot of control over how sort behaves. A block object, like any other object, can be stored in a variable, passed along to other methods, or even copied.
Many programming languages support code objects like this. They’re called closures and they are a very powerful feature in any language, but they are typically underused because the code to create them tends to look ugly and unnatural.
A Ruby block is simply a special, clean syntax for the common case of creating a closure and passing it to a method. This simple feature has inspired Rubyists to use closures extensively, in all sorts of creative new ways.
Ruby contains many advanced features.
- Exceptions for error-handling.
- A mark-and-sweep garbage collector instead of reference counting.
- OS-independent threading, which allows you to write multi-threaded applications even on operating systems.
You can also write extensions to Ruby in C or embed Ruby in other software.
Table of Content
- History of Ruby language
- Install Ruby on MacOS
- Install Ruby on Ubuntu
- Install Ruby on Windows
2. Ruby Basic
- Syntax and data types
- String and Text
- Split string into array
- Blocks and Iterators
- Control Structure if/unless
- Control Structure elsif/case
- Control Structure while/until
- Regular Expressions
3. Classes Objects and Variables
- Objects and Attributes
- Global Variable
- Instance Variable
- Class Variable
- Methods of the class
- The default scope in class
- Override a method
- Reflection a method
- Nested class
- Standard Libraries
- Install from RubyGems
- Create your own gem
- Create a document with RDoc
- Exception Handling and Debug
4. File I/O and Database
- I/O basic with keyboard
- Open the file and write/read
- Rename and delete the file
- Create and delete the directory
- Database based on text files
- Write/read the CSV file
- Object storage using PStore
- Object storage using YAML
- Use SQLite in Ruby
- Connect with MySQL and PostgreSQL
5. Multitasking programming in Ruby
- Introduction to Fibers
- Scheduler for the process
- Mutual Exclusion
- Running Multiple Processes
6. Network programming with Ruby
- Download the webpage
- The Net/Http library
- The Open-Uri library
- HTML parsing with Nokogiri
- XML parsing with REXML
- Receive email with POP3
- Send email with SMTP
- Transfer the file with FTP
- Socket programming TCP/UDP
- DNS query
- Server-Client programming