"Welcome " + "to " + "Odin!" #=> "Welcome to Odin!" In true Ruby style, there are plenty of ways to concatenate strings. Specifically, string interpolation and the escape characters that we’ll discuss soon both only work inside double quotation marks, not single quotation marks. They are pretty similar, but there are some differences. Strings can be formed with either double "" or single '' quotation marks, also known as string literals. What were you thinking, Burt? Double and Single Quotation Marks Strings, strings, wonderful things, use them well and…your app will…grow wings? Or something.Īt first glance, you might think that strings are just a bunch of characters that aren’t very useful beyond getting user input and outputting some information to the screen, but like Burt Reynolds passing up the chance to play Han Solo, you’d be wrong. There are many useful methods for numbers built into Ruby. Ruby doesn’t do any rounding in this conversion. # To convert an integer to a float:Īs shown in the last example above, when Ruby converts a float to an integer, the decimal places are simply cut off. Ruby makes it very easy to convert floats to integers and vice versa. To obtain an accurate answer, just replace one of the integers in the expression with a float. It’s important to keep in mind that when doing arithmetic with two integers in Ruby, the result will always be an integer. Floats are numbers that contain a decimal point, such as 10.5, 10.0, or 0.25. There are two main types of numbers in Ruby. # Modulus (find the remainder of division)ġ0 % 4 #=> 2 (10 / 4 = 2 with a remainder of 2) Ruby has all the typical math operators you would expect: # Addition You probably already know what numbers are, so there’s no need to go into elaborate metaphors here. Explain what the Booleans true, false, and nil represent.Define what a symbol is and how it differs from a string.Describe what escape characters are, and list several examples.Explain string interpolation and concatenation.Describe the difference between an integer and a float and how to convert between the two.List the basic arithmetic operators and what they do.Learning Outcomesīy the end of this lesson, you should be able to do the following: We’ll start here with four of Ruby’s basic data types: numbers (integers and floats), strings, symbols, and Booleans ( true, false, and nil).įor all of the examples throughout this lesson, feel free to follow along in irb or (an online REPL environment) to get a better feel for how they work. Ruby is a strongly object-oriented language, which means that absolutely everything in Ruby is an object, even the most basic data types.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |