1. Home
  2. Computing & Technology
  3. Ruby

Using Variables in Ruby- -Assign Information to a Variable

By , About.com Guide

3 of 3

Use the "Puts" Function

The simplest and most frequently used example in any programming language is the "Hello World" application. Its purpose is to print a simple message to the screen, usually "Hello, world." This is achieved in Ruby with the puts or "put string" function. The puts function will write to the screen any information it is given, including the information stored in any variables it is given.

In the first statement of this program, the information "Hello there" is assigned to the variable called message. In the second statement, the variable called message is passed to the function puts.

"Passing" a variable to a function is another way of saying you're giving a copy of the information stored in the variable to the function for use. The puts function is given the message "Hello there" which was stored in the variable called message. The puts function will then take this message and write it to the screen.

irb(main):001:0> message = "Hello there"
=> "Hello there"
irb(main):002:0> puts(message)
Hello there
=> nil
Explore Ruby
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Ruby
  4. Tutorials
  5. Using Variables in Ruby--Assign Information to a Variable

©2010 About.com, a part of The New York Times Company.

All rights reserved.