Introduction to Ruby
Learn about Ruby, an object-oriented scripting language you can use on its own or as part of the Ruby on Rails web framework.
StartKey Concepts
Review core concepts you need to learn to master this subject
Ruby Variables
Put and print command
Code comments in Ruby
Numeric data types in Ruby
Arithmetic operations in Ruby
Ruby Object Methods
Strings in Ruby
Boolean Data Types in Ruby
Ruby Variables
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
.
- 1Ruby 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…
- 2In 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 …
- 5The 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…
- 6Because 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…
- 7Methods 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…
- 8The .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”….
- 9Let’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.
- 10You 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…
- 11You 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…
- 12There 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…
- 13Let’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.
- 15Well done! Let’s do a little review of string methods. Remember, you call a method by using the . operator, like this: “string”.method.