Introduction to Ruby
We programmers are lucky to be working today. I say this because there are so many excellent programming languages from which to choose, especially in the Open Source world.
One of the most talked-about languages is Ruby. Ruby isn't actually all that new. Yukihiro “Matz” Matsumoto released the first public version in 1995, and it has grown in popularity ever since. As the Ruby on Rails framework for Web development has become increasingly popular, interest in Ruby has soared along with it.
Ruby often has been described as a cross between Perl and Smalltalk, and I don't think this is a bad way to look at it. Certainly, if you have experience with both Perl and object-oriented programming, you probably will feel right at home working with Ruby.
In this article, I introduce the basics of Ruby, showing how it is similar to other high-level languages and where it adds its own, special twist. By the end of this article, I hope you'll know enough about Ruby to try it out for some small applications. If you're like me, you'll quickly discover that Ruby is surprisingly compact and elegant, making it possible to write maintainable code quickly and easily.
Downloading and installing Ruby is fairly easy, particularly because a recent version (1.8.2) is included with many distributions of Linux. You either can use that version or install the latest version (1.8.4) from the main Ruby site. As an open-source product, you shouldn't be surprised to find that the main Ruby site (www.ruby-lang.org) offers the source code in .tar.gz format. Additional formats, such as RPMs and Debs, are available from the official repositories for your favorite distribution.
If you want to install the latest version of Ruby from source, download and unpack the .tar.gz file:
$ cd Downloads $ tar -zxvf ruby-1.8.4.tar.gz
Now use the standard configure program to find the system configuration automatically, make to compile it and then make test to ensure that the compiled version of Ruby works correctly:
$ ./configure && make && make test
If all goes well, the final line of output from the above commands will read test succeeded. Now you can become the root user and install Ruby onto your system:
$ su # make install
The Ruby language itself exists as an executable called ruby, which you can run manually by typing it on the command line:
However, this version of Ruby is designed for non-interactive use. To test code or experiment with the Ruby language, there is irb, the interactive Ruby shell. Irb is something like a debugger, in that it takes input from a user (terminated by pressing the Enter key) and executes it. For example, type:
And, irb responds with its prompt:
Now we can type a bit of Ruby:
irb(main):001:0> print "Hello, world"
And, irb responds with:
Hello, world=> nil
The above output indicates that print displays Hello, world on the screen and returns a nil value; nil is Ruby's way of representing a null value, much like undef in Perl, None in Python and NULL in SQL.
Like many other high-level languages, Ruby allows us to assign values to variables without pre-declaring them. Thus, we can write:
greeting = "Hello, world" print greeting
Ruby also can do math, using the familiar operators +, -, * and /:
5 + 3 60 - 23 60 * 23 10 / 2
I have omitted the call to print in the above lines, because it's unnecessary in irb. However, in a standalone Ruby program, no output would be sent to the screen (or elsewhere) without using print.
If you are a seasoned Perl programmer, you might be somewhat surprised to discover the result of the following:
5 / 2
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Back to Backups
- A New Version of Rust Hits the Streets
- Google's Abacus Project: It's All about Trust
- Secure Desktops with Qubes: Introduction
- Seeing Red and Getting Sleep
- Fancy Tricks for Changing Numeric Base
- Secure Desktops with Qubes: Installation
- Working with Command Arguments
- CentOS 6.8 Released
Until recently, IBM’s Power Platform was looked upon as being the system that hosted IBM’s flavor of UNIX and proprietary operating system called IBM i. These servers often are found in medium-size businesses running ERP, CRM and financials for on-premise customers. By enabling the Power platform to run the Linux OS, IBM now has positioned Power to be the platform of choice for those already running Linux that are facing scalability issues, especially customers looking at analytics, big data or cloud computing.
￼Running Linux on IBM’s Power hardware offers some obvious benefits, including improved processing speed and memory bandwidth, inherent security, and simpler deployment and management. But if you look beyond the impressive architecture, you’ll also find an open ecosystem that has given rise to a strong, innovative community, as well as an inventory of system and network management applications that really help leverage the benefits offered by running Linux on Power.Get the Guide