Ruby


Introducing Ruby

Ruby is a powerful, yet easy-to-learn object-oriented programming language with a nice clean syntax. It was invented in Japan in the mid-nineties, but has really taken off in recent years, largely thanks to the Ruby on Rails framework, which lets you easily write powerful dynamic websites and web apps in Ruby.

As well as being popular with web developers, Ruby is also used for many other purposes, including system administration tasks and writing GUI-based desktop apps for Windows, Mac OS and Linux.

Ruby is, by default, an interpreted language, much like PHP, Perl and Python. This means that you need to install a Ruby interpreter to process and run your Ruby programs and scripts.


Who uses Ruby?

Ruby is an incredibly powerful and highly scalable object-oriented language. Leading technology companies & startups around the world use Ruby or the Ruby on Rails framework to power their websites & web applications. They include:
* Amazon
* Twitter
* Electronic Arts
* Yahoo
* New York Times
* 37 Signals

* And many many more...



Installing Ruby


In order to start programming in Ruby, you first need to install the Ruby interpreter on your computer. The Ruby interpreter is the program that takes your Ruby script files and runs them.


Writing a simple Ruby script


Now that you have the Ruby interpreter installed on your computer, let's try it out by writing a very simple Ruby script and running it.

Using your favourite text editor, create a new text file called greeting.rb. Type the following text into the file

Now save the file.

Let's work through this script line by line, and see what it does:


  1. Display a prompt.

The first line of code displays the message "Hello, what's your name?" on the screen. To do this, it calls a method named puts, passing the string argument "Hello, what's your name?" to the method. If you're not familiar with object-oriented programming, a method is a piece of code attached to a class, object, or module. When you call a method (by typing its name in your script), Ruby runs the code inside the method. The puts, or "put string", method happens to be attached to a built-in Ruby module called Kernel, and its job is to output its string argument to the terminal window.

 2. Get the user's name.

Now that we've prompted the user to type their name, we need to read the text that they enter on the keyboard. We do this by calling the gets method in line 2. This method is the opposite of puts; whereas puts outputs a string of text, gets reads in a string of text typed in by the user, and returns it. We then take this string value, which is actually an object (more on that in a moment), and call the object's chomp method; this simple method merely removes the newline from the end of the string (created when you press Return on your keyboard to enter the string). We then store the resulting string in a new variable called name.

 3.  Display a greeting.

Finally, line 3 displays a greeting message to the user. The message consists of the string "Hi there, ", followed by the user's name (stored in the name variable), followed by an exclamation mark (!). Again, we use the puts method to display the greeting. We also use the concatenation operator, +, to join the three strings together to produce the greeting string.

Running the script

Try this script out!
Open your terminal window and use the cd command to change to the folder holding your greeting.rb file. Type the following:

ruby greeting.rb

Then press Return. You should see the following message:



Type your name, then press the Return (Enter) key on your keyboard. You'll see a message similar to the following:



Congratulations — you've just created and run your first Ruby script!

Comments

Popular posts from this blog