Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Ruby, Hello World Introduction

hello world

  • Please log in to reply
No replies to this topic

#1 RhetoricalRuvim


    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 27 August 2011 - 03:53 PM

Ruby is a high-level language that is usually interpreted, and not compiled.

An interpreter is used to interpret languages like Ruby. An Interpreter is a program that looks at the code and does whatever the code says.

A compiler doesn't do what the code says, but rather converts that code to machine code, so that the computer would do what that code says, when it's time for that code to run.

In other words, a compiler makes source code more readable for the machine, while an interpreter runs the source code on-the-spot.

  • Getting Started
  • Example Program

Getting Started

The Tools
Let's take a look at what tools we would need.

If you want to use the Ruby Installer, go to RubyInstaller.org Downloads.

Under RubyInstallers, use the link with the latest version (as of this writing, it's 'Ruby 1.9.2-p290').

When you're installing ruby, you might like to install it in the C:\ directory (not in program files), for easier access.

Posted Image

(Fullsize Screenshot)

It might also be a good idea to associate the .rb and .rbw file exensions with the Ruby Interpreter.

When you're making web scripts using Ruby, you might have to include the path to ruby. That's done by putting a '#' sign, followed by a '!', followed by the path to ruby; for example, if the path to ruby (in our case) is C:\Ruby\bin\ruby.exe - which usually can be said as \Ruby\bin\ruby.exe - then we would probably type something like this, at the beginning of the .rb file:

That tells the server that the interpreter for this script is at that path.

Example Program

The Plan
Okay, it's time to get to the example program. Here's the plan:
  • Print "Hello World!" to the screen.
  • Wait for the user to press the return (enter) key and exit.

Let's go over what we'll do, to achieve the plan above.

A Little About The Language
Functions, in Ruby, don't have to be called with parentheses around the arguments, as long as it's understandable what's happening.

So first of all, there's the 'puts' function; it means 'put string' .

There's also the 'gets' function; that means 'get string' .

The puts function sends its first argument, followed by a new-line character (/sequence), to the standard output. So if you call puts() with the "hi" string, the output would actually be "hi\r\n" and if you call puts() with the "abcd" string, the output will be "abcd\r\n" .

The gets function gets a string from standard input, until the first carriage return character, and returns that string.

Besides puts() there's also print(), which sends its parameter to standard output, but does not append a new-line character sequence to the output. So PRINTing "hi" would really print "hi" and not "hi\r\n" .

Standard output is usually directed to the screen, and standard input is usually directed from the keyboard; however, in a web environment (with a web server), the standard input comes from the HTTP request that's sent by the client (browser) and the standard output gets sent back to the client (usually as the HTML code for the browser to view).

And another thing, before we go on to the code, strings, in Ruby, are represented by either single- or double-quotes.

When in double-quotes, the string can include things, if you use the '#' character, then the '{' and '}' characters:
"Hello #{username}, how are you? "

would return the string with 'Hello ' and whatever's in the variable `username` and then the string ', how are you? ' . So if `username` is "user1" , the string will evaluate to "Hello user1, how are you? " . You can't do that with single-quote strings. We would go over variables later.

Comments, in Ruby, are denoted by the '#' (sharp) character. The sharp character tells ruby (the interpreter, not the language) to ignore the rest of the line (the rest of the line is for people who read the code, so they can understand what the code does).

The Code
Here's the code:
puts 'Hello World!' 

And that's it!

puts 'Hello World!'
sends "Hello World!\r\n" to the standard output and
waits for the standard input (in this case, for a return key press).

The Output
Posted Image

(Fullsize Screenshot)

First Tutorial:
This is the first tutorial.

Previous Tutorial:
Not Applicable

Next Tutorial:
Variables and Functions
  • 0

Also tagged with one or more of these keywords: hello world

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download