Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to Ruby
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!

Ruby Data Types (Part 1)

  • This lesson will go over common ruby data types. The first part goes over numbers, strings, and symbols.
  • All number objects are instances of Numeric
  • Strings are mutable objects
  • RDoc:
  • Strings with double-quotes allow more flexibility than with single-quotes
  • String interpolation allows global, instance, or class variables
  • Strings in 1.9 are a true sequence of characters
  • Ruby contains a symbol table that stores names for all classes, methods, and variables
  • RDoc for Symbols:

Ruby Data Types (Part 1)

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Overview 0:10
  • Ruby Data Types 0:10
    • Numbers
    • Strings
    • Symbols
  • Numbers 0:30
    • Numeric
    • Float
    • Complex
    • BigDecimal
    • Rational
    • Integer (most popular) - Fixnum and Bignum
    • Fixnum stores 31 bits
    • Bignum stores larger bits
    • All number objects are instances of Numeric
  • Integer Literals 2:28
    • Represent whole-numbers
    • Examples - Different bases
    • Binary
    • Octal
    • Hexadecimal
    • Examples
  • Floating Point Literals 4:45
    • Examples
    • e-value can be capital or lowercase
    • Example
  • Strings 6:16
    • Mutable objects
    • Used for inserting and deleting text, searching, and replacing
  • String Rdoc 6:46
    • Definition
  • String Literals 8:20
    • Single-Quoted
    • Double-Quoted (most used)
    • Example
    • Escape Sequences
    • Newline
    • Tab
    • Double quote
    • Blackslash
    • Interpolation
    • Sprintf
    • Unicode Escaping
    • Example
    • Delimiters
    • Here Documents
    • Example
  • String Operators 19:58
    • Concatenation
    • Appending
    • String Equality
    • Example
  • Substrings 22:00
    • Range object (inclusive)
  • String Encoding 24:52
    • Differences between Ruby 1.8 and 1.9
  • Symbols 26:02
    • Definitions
    • Examples
    • When to use symbols
  • Symbols and Strings 27:42
    • Symbols Rdoc

Transcription: Ruby Data Types (Part 1)

Welcome back to

Today's course is Ruby datatypes.0001

This is the Part 1 lesson.0004

We are going to go over three datatypes today.0006

The first one is numbers, and then strings, and then symbols.0012

First, we are going to look at number.0027

There are a lot of children that inherit from the numeric class.0029

Numbers have a lot of objects that deal with them.0036

First, we have our Float class.0042

It's a child of Number.0046

And then we have this Complex object, BigDecimal, RationalObject, Integer.0049

So we have five objects: Float, Complex, BigDecimal, Rational, and Integer.0062

Integer has two children that inherit from it.0069

We have Fixnum, and then we also have Bignum.0075

All of these number objects are actually instances of Numeric.0081

The first one is Float.0093

One thing that you will notice about Float is that it uses native floating-point representation of whatever platform it's on.0099

The other big on is Integer; that is going to be the most popular one to use.0110

These are just regular digits.0117

Fixnum stores a smaller amount--31 bits.0121

If it's ever an integer that is greater than 31 bits, it is going to be upgraded to a Bignum object.0133

We are going to look at the Integer class now--at Integer literals.0146

This is part of the Integer class, and they represent whole numbers.0149

Here we have a zero--that's an integer--1, 2, 3, and this huge number here--that's all part of the integer class.0160

There are also different types of integers based on the base type.0175

So, we look at binary base-2--you can actually do that with this.0179

Our base-2 object--you want to look at this 0b.0188

When you call that 0b, that knows it's a base value, and it is going to render 375.0194

Now, we look at the Octal class.0205

That's base-8.0209

For example, you call up 0, 5, 6, and 7--it's going to be 375.0215

And then we look at our hexadecimal--our base-16.0220

We have our 0x177--it's going to be 375, too.0229

Let's do an example of that.0236

If I do...0, 3, have notice this is part of the Octal.0243

I could also do Binary, so it would be 0000...10--it's two.0259

I could also do hexadecimal--0xa--you would get a ten.0269

So, those are the integer literals.0279

The next thing we want to look at is the floating-point literals.0285

This is part of the Float class.0290

Examples of a Float: this is 0.0--it's a floating-point value--3.14159--notice that both of these have a period there--that's how it knows to make it a Float object.0293

Then we have this E value, which makes it ten cubed.0314

Now, the exponent value can be the letter e or capital E.0320

There is a max constraint to floating-point.0335

I go here, and I put in floatmax--it's actually a pretty hefty number.0341

It might say 1.79, but you have to look at this e--that's e to the power of 300a.0352

So this floating-point value can go quite high.0357

It should handle plenty of your cases.0361

The next thing we are going to look at is strings.0372

Strings are mutable objects: you can change them.0375

They have a lot of operators and methods.0381

There are definitely a lot of things you can do with strings.0384

Inserting and deleting can do searching in there...replacing text...and there are a lot more options.0387

Check out the RDoc.0397

As you see here, it says, "A String object holds and manipulates an arbitrary sequence of bytes--typically representing characters."0406

"String objects may be created using String::new or as literals."0416

"Because of aliasing issues, users of strings should be aware of the methods that modify the contents of a String object."0425

Typically, methods ending in this exclamation mark--or bang value--that means modify their receiver, "while those without the '!' return a new String."0431

"However, there are exceptions, such as String # [] =."0442

Now this "!"--what it will do is modify the existing object, so when you have that "!" value, it is not going to create a new object.0450

Some interesting things here: new--this is our constructor to make a String object; try_convert--this, I believe, is only 1.9.3, but what this does is, if I put in a string in this value, if it allows it to convert that to String, it will be String, but if it doesn't allow this regular expression will just say, "This is nil. I'm not going to allow that in."0459

Let's look at some of the string literals.0499

You have your single-quoted strings--you will notice that it has single quotes in our value.0502

Notice, with this one, we have to put a slash in front, because it's another single quote in a single quote.0517

The most popular string literal you will be using is the double-quoted one.0530

There is a lot more flexibility--you don't have to worry about the slash here--you have your double quotes.0533

This should all be on one single line.0541

If you want to do a new line with that, it will not work0546

You can try it out; we can test it.0552

But remember, the new line is going to terminate this.0554

So, let's open a new one.0562

First, I want to look at the single quotes.0565

You can actually span these lines to multiple lines.0569

I have an example: 'This is my sentence.'0572

Let's say I want to make a paragraph with this.0577

I can use this forward slash here and have that spanned even further.0581

'This is my second sentence.'0588

I can keep going on to more sentences.0600

What I want to show you is that, when I press Return here, this is when it's going to actually end it.0606

Notice here--this is a Return--this forward slash is allowing me to expand this all into one line.0611

There is no end break here.0619

Also, for double-quoted values, let's try the example we just tried.0623

It might cause an issue...but if I do "This is my sentence"...have a double Return...interesting! Look, it actually puts a quote here!0630

This says, "I know you haven't ended your string yet; continue until you do it."0640

"This is my second sentence."0646

I put my double quote; press Return; and look there! It actually recognized it and put a new line.0649

So it does allow it...that's interesting.0656

Let's look at some of the string literals; we just saw one, actually.0668

For the escape sequences, we just saw the new line.0671

You could also do the tab.0677

It's going to add another space--a tab--in your actual quote.0681

We have our double quote: you notice that all it is, is we are putting this slash in front to make it a double quote, and we also have our backslash.0686

The reason is, if we want to put double quotes in a double-quoted string literal, you have to have that have to add a backslash to display that.0697

One very fancy thing with Ruby is that they support interpolation in the strings.0711

We're going to go over that now.0721

For example, we have this total=100+150.0722

There is our code--but I want to get this value and print it out.0725

So, look at my next one: I put the total in this hashtag and this curly brace--this is going to allow me to put Ruby code in here.0733

I can put Ruby into these codes.0749

It's smart enough to say, "Hey, this total value is already being stored in this identifier, total, but it has a value of 350"--it's going to know that it is an integer value and make that a string so it can read it.0756

So, the end output will say the total is 350.0773

Again, you could use this with anything.0778

The scope--for example, it worked for globals, instance, class variables...depending on the scope you're on, you can use a dollar sign total...this instance, you can even put..."at" total, or class variable, 0785

I want to do that here,

One very interesting thing about Ruby is that it does allow sprintf.0826

Printf/sprintf are very popular methods that are used in many other languages.0834

It is not something you want to use in Ruby, but it is there; it gives you the flexibility to use it if you want to.0841

It does help to make the transition easier to Ruby.0848

So, we have our sprintf--our string value--here, and we have our percentage, which says this is a Float.0852

And we can just pass in math, the two dots, two dots, and pi, and that will render this value into the Float.0861

Strings also support Unicode escaping.0873

You just have to add this slash, and the u, plus the four-digit hexadecimal digit.0881

In this case, we have 0041; it says "A".0890

But interestingly enough, with the string interpolation, we can actually remove the leading zeros.0896

Here, we didn't even put 0041; it's just forward slash, u, we have our hash, curly brace, and we just put 41.0916

It's smart enough to know that you are using leading zeros there--"I can interpolate that in and render out the same tag--I can render out the A value."0925

Let's go ahead and try it out in our terminal here.0940

For example, we can just make it ourselves, and just use 0024--notice this dollar sign--but I can also use the interpolation, and it will also show a 24 value.0949

Also, you don't need to use quotes; you can also show it using just delimiters.0978

It's very flexible: you can use parentheses, square brackets, curlies, greater than/less than, dash-dash, pipes...0986

Anything...particularly special characters, where you have another one at the end--it will support it.0998

For example, here you have the single-quoted string rule.1003

We also have a double-quoted string rule.1010

Notice, the single quote is in lowercase; the capital Q means it's double-quoted.1014

And another one that follows the double-quoted string rule--that makes it very simple--is this percentage value.1020

Before we do that, I do want to show you some...1036

See if it can go very quickly on, if I say this is a will notice that this is following the single-quoted values.1041

But let's say this is value, and I want to put my token in here, notice it takes it literally?1055

Token is just taken as part of that text.1068

But I can say token=abc, and when I take that exact same one and make it part of the double-quoted rules, it will interpolate it: so it wil say "value is abc."1075

Next, we want to go over the string literals.1091

We are going to look at heredoc.1096

This is used for multiple-line documents.1102

It makes it very easy to show all your text, because you can preserve all the spaces and things that you put between it.1108

To start it off, notice, I use this "less than", "less than"1121

This token tells Ruby, "Hey, this is a heredoc; this is a comment here; I'm not going to worry about how I'm going to parse this out," because once I put this "less than", "less than" here, that is saying this is a string.1132

"So this string--I'm going to store it as a value until I see this here token again."1154

That is where we see here also.1161

So, it says, "Any value that is between these two pieces--I'm going to take that as a string value, and I'm going to store it into this lorem ipsum text."1164

I wouldn't recommend using heredoc; it's very popular in other languages, but it's not a Ruby convention that is really followed.1181

But there are reasons to use it, especially if you have a big text piece--so use it at your own flexibility.1187

Next, we are going to look at public string operators.1195

This one is just concatenation.1201

I have our string: hello, space, hello world.1204

When Ruby sees this, it's going to create a new object that has our hello world here.1210

Now, this object that it creates--it isn't going to append it to hello, to the quote, or world; it's going to create a brand-new object and return that.1219

Now, if you use appending, and use these less than-less than, it will actually store that at the end of the value.1236

This x value appends this quote, and it appends world; you will get the exact same value, hello world, but this is actually part of the x object.1244

Here is another operator string quality: you have your equal equal, and your not equal.1262

Equal equal just checks if two values are the same.1271

Not equal says if it isn't or it is.1274

If I have x=abc, y=abc, x=y, you will notice that it returns "true."1282

Let's say it's not equal: it returns "false."1295

Very popular; very simple to use, too.1299

Also notice, with the x=abc, y=abc, it does return the final object, which is something in Ruby.1302

Next, we want to look at the substrings.1319

We have our value:

If you look at this second one here...we are going to get a substring of it.1327

So we use these square brackets, and we say 0..7; this is a range object, and it's inclusive.1334

What this says is, "Starting from the first value (which is zero), get me the whole value all the way until I get to the seventh index."1357

We have our value 0 here...let me put that here...that is zero, and here is our value 7.1367

It gets from 0 to 7, and it will return "educator."1380

There is another way to do it, and this is with square brackets.1385

This one, with the square brackets, says, "Start at this index."1389

It says, "I'm going to start at index 0."1400

Again, 0 index is the e, and it says, "Give me the eight characters starting from that index."1403

We are getting our eight characters--1, 2, 3, 4, 5, 6, 7, 8--so we have "educator."1426

The last one I want to go over, a substring, is--we can actually go back to the last text to the front.1434

Here we have our a-1.1441

This will actually look at the last value of that string.1446

It starts from the last indexed value.1451

If I were to do a-2, that actually returned the o.1473

Next, we want to talk about string encoding.1489

First, with Ruby 1.8, strings are a sequence of bytes.1491

This makes it tougher to do encoding--it makes it part of a byte schema, and it follows an ASCII character set.1502

Now, in 1.9, strings are actually a true sequence of characters.1525

They also have an encoding method as part of it.1530

I can specify if I want it ISO, UTF-8, or some other encoding.1539

It's not confined to the ASCII character set.1546

That is one reason we are using Ruby 1.9.1556

The last thing we want to go over is symbols.1559

Symbols represent names and strings in the Ruby Interpreter.1564

They are stored in a table that stores names for the classes, methods, and variables.1570

This allows the Interpreter to void most string comparisons.1578

It also makes it very easy for it to display these values onto the screen, and to do a lookup without having to worry about where these objects come from.1584

For example, I have this "symbols"; a symbol is basically...this is the basic one.1600

Another way to declare it is with two dots and a double quote; two dots and a single quote; or the percentage, s, and the parentheses.1611

The basic way is the first one--this is how we would mainly use that.1626

You want to use symbols in cases where the object doesn't require to be mutable.1639

For example, if you have a string versus a symbol, you always want to go with symbols first, because it's more efficient.1648

Another way with symbols and strings--you can actually convert them back and forth.1660

So, I have this symbol here; I can call this method to_s, and that will convert it to a string.1665

Again, with a string, I can use to_sym, and it will convert that to a symbol.1675

Let's look at the RDoc for this Symbol class.1690

What we are looking at here, notice, has some description about Symbol.1701

It says it is "generated using the :name and :"string"," and you have the to_sym method.1712

Now, what is very interesting is, looking at this example here, we have our module One, class Fred.1719

And you will notice that you have this class, a symbol, :Fred, and then you have another global object, f2, that has Fred.1731

And, at the bottom, all of them actually have the same object ID.1745

Think of this versus if I were to create a string called Fred, declare that in f1, then declare a string in f2--Fred--then a string in f3.1750

They would have different object IDs, whereas the syms, they all use the's very much more efficient, and a better use of the way it's stored in Ruby.1766