Variables and Types


A variable is a named piece of data. You can use a variable to store a value within your program, and refer to it later by the variable name.

Variables are defined using =:

myFirstVariable = "I made a variable!"

Once you have assigned a value to a variable, you can then refer back to it:

puts myFirstVariable

There are three main types of variable:

  • Strings (a collection of symbols inside speech marks)
  • Booleans (true or false)
  • Numbers (numeric values)

    aString = "I'm a string!" aBoolean = true aNumber = 42

Exercise

Define three variables, myString, myBoolean, and myNumber, and set them to "I'm programming!", "true", and "5".


Copyright © learnrubyonline.org. Read our Terms of Use and Privacy Policy