Key Concepts

Review core concepts you need to learn to master this subject

Ruby Variables

myVar = 48

In Ruby, a variable is a place to store values of almost any type including Integer, Boolean, String, Array, and Hashes.

Each variable has its own name which cannot begin with a capital letter or a number and we use the equal sign for assigning a value to that variable.

The variable declaration does not require that you mention a specific data type.

The following program declares myvar variable and assigns the value 48.

Introduction to Ruby
Lesson 1 of 2
  1. 1
    Ruby is a powerful, flexible programming language you can use in web/Internet development, to process text, to create games, and as part of the popular Ruby on Rails web framework. Ruby is: * **Hi…
  2. 2
    In Ruby, your information (or data) can come in different types. There are three data types in Ruby that we’re interested in right now: Numeric (any number), Boolean (which can be true …
  3. 3
    One of the most basic concepts in computer programming is the variable. You can think of a variable as a word or name that grasps a single value. For example, let’s say you needed the number 25…
  4. 4
    Ruby isn’t limited to simple expressions of assignment like my_num = 100; it can also do all the math you learned about in school. There are six arithmetic operators we’re going to focus on: Addi…
  5. 5
    The print command just takes whatever you give it and prints it to the screen. puts (for “put string”) is slightly different: it adds a new (blank) line after the thing you want it to print. You us…
  6. 6
    Because everything in Ruby is an object (more on this later), everything in Ruby has certain built-in abilities called methods. You can think of methods as “skills” that certain objects have. F…
  7. 7
    Methods are summoned using a .. If you have a string, “I love espresso”, and take the .length of it, Ruby will return the length of the string (that is, the number of characters—letters, numbers, s…
  8. 8
    The .reverse method is called the same way .length is, but instead of asking Ruby to tell you how long a string is, it spits out a backwards version of the string you gave it. For instance, “Eric”….
  9. 9
    Let’s try one more method (er, two methods). As you might have guessed, the .upcase and .downcase methods convert a string to ALL UPPER CASE or all lower case, respectively.
  10. 10
    You probably saw us use the # sign a few times in earlier exercises. The # sign is for comments in Ruby. A comment is a bit of text that Ruby won’t try to run as code: it’s just for humans to r…
  11. 11
    You can write a comment that spans multiple lines by starting each line with a #, but there’s an easier way. If you start with =begin and end with =end, everything between those two expressions w…
  12. 12
    There are many different kinds of variables you’ll encounter as you progress through these courses, but right now we’re just concerned with regular old local variables. By convention, these var…
  13. 13
    Let’s quickly review how to declare and set variables. Remember, you declare a variable just by saying its name, and you set it using =. You can always check the Hint below if you need more help.
  14. 14
    Good! Now let’s do a little math.
  15. 15
    Well done! Let’s do a little review of string methods. Remember, you call a method by using the . operator, like this: “string”.method.
  16. 16
    Great work! For our last review exercise for this lesson, let’s go over single- and multi-line comments.