1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Using Variables in Ruby--Assign Information to a Variable

By

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
 
  1. About.com
  2. Technology
  3. Ruby
  4. Beginning Ruby
  5. Using Variables in Ruby--Assign Information to a Variable

©2014 About.com. All rights reserved.