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.

Put and print command

myVar = 48

put and print commands can be used to display text in the console.

Code comments in Ruby

myVar = 48

Commenting code helps programmers write free text that is commonly used to explain the code written, or can even be used to add TODO’s to the code. There are two types of comments that can be written in Ruby:

  • Single line comments start with a #.
  • Multi line comments start with =begin and end with =end.

Numeric data types in Ruby

myVar = 48

In Ruby, the Numeric data type represents numbers including integers and floats.

Arithmetic operations in Ruby

myVar = 48

In Ruby, we can use arithmetic operators to evaluate mathematical expressions. The most common Ruby arithmetic operators are addition (+), subtraction (-), division(/), multiplication(*), exponentiation(**) and modulo(%).

Ruby Object Methods

myVar = 48

In Ruby, methods are built-in abilities of objects. To access an object’s methods, you need to call it using a . and the method name.

Strings in Ruby

myVar = 48

Strings in Ruby are a sequence of characters enclosed by single quotation marks (‘’) or double quotation marks (“”).

Boolean Data Types in Ruby

myVar = 48

In Ruby, in order to represent values of truth about specific statements, we use Boolean variables. Boolean variables values are either true or false.

Ruby .upcase and .downcase Methods

myVar = 48

Ruby strings have an .upcase and a .downcase method used to change their case. .upcase returns a version of the string in all uppercase, and .downcase returns a version in all lowercase.

Ruby string interpolation

myVar = 48

In Ruby, string interpolation is used to insert the result of Ruby code into a string.

Get user input in Ruby

myVar = 48

In Ruby, we can get the user’s input using gets.chomp. gets is the method that is used to retrieve user input. Ruby automatically adds a new line after each bit of input, so chomp is used to remove that extra line.

Chevron Left Icon
Introduction to Ruby
Lesson 1 of 2
Chevron Right Icon
  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.
  1. 1
    This project will help you create a small program that will read a user’s input and correct his or her capitalization. Users can provide an almost infinite range of input, so it makes our lives eas…
  2. 2
    First, let’s write the code we’re already familiar with. In order to get input from the user, we’ll first need to print a prompt on the screen.
  3. 3
    Good! Now let’s try something new. You may have noticed this weird little line of code repeated in our example: variable_name = gets.chomp gets is the Ruby method that gets input from the user. …
  4. 4
    All right! Now we need to repeat what we’ve done for last_name, city, and state.
  5. 5
    As you might have noticed, Ruby’s not really returning any feedback to us. We want to be able to see our string formatting in action! For this, we’ll need one more new piece of syntax. If you defi…
  6. 6
    Great! Now we’ve got our output, but as you can see, we haven’t used string methods to properly capitalize everything yet. print “This is my question?” answer = gets.chomp answer2 =…
  7. 7
    Perfect! In just a few short steps, you’ve created your own Ruby program with real-world applications. Feel free to add to your program or change it as much as you like.