Key Concepts

Review core concepts you need to learn to master this subject

Get user input in Ruby

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.

Ruby string interpolation

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

Code comments on Ruby

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.

Put and print command

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

Arithmetic operations in Ruby

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

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.

Ruby variables

In Ruby, variables are used to store data of almost any type including Integer, Boolean, String, Array, and Hashes.

Ruby Variables

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.

Strings in Ruby

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

Boolean Data Types in Ruby

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

Numeric data types in Ruby

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

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 […] ; it can also do all the math you learned about in school. There are six arithmetic operators we’re going to focus on: Addition …

  5. 5

    The […] command just takes whatever you give it and prints it to the screen. […] (for “put string”) is slightly different: it adds a new (blank) line after the thing you want it to print. Y…

  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, […] , and take the […] of it, Ruby will return the length of the string (that is, the number of characters—letters, numbers, space…

  8. 8

    The […] method is called the same way […] 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, […] …

  9. 9

    Let’s try one more method (er, two methods). As you might have guessed, the […] and […] 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…

  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 […] and end with […] , everything between those two exp…

  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 mo…

  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: […] .

  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 […] 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: […] […] is the Ruby method that gets input from the user. When getting inpu…

  4. 4

    All right! Now we need to repeat what we’ve done for […] , […] , and […] .

  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. […] 1. First we introduce one new method, […] , here. It capitali…

  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.