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 Educator.com

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 2)

Ruby Data Types (Part 2)

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
  • Ruby Data Types 0:15
    • Boolean
    • Arrays
    • Hashes
    • Range
  • Boolean Types 0:42
    • TrueClass
    • FalseClass
    • NilClass
    • TrueClass Examples
    • FalseClass Examples
  • Arrays 4:16
    • Ordered collection of objects
    • Can hold different objects
    • Starts at index 0
  • Array of Strings 5:50
    • Example
  • Arrays (Cont.) 6:20
    • Can be created using literals
    • Can be created using constructors
    • Position and indexed value
    • Negative Indexed Values
    • Shift and Unshift
    • Push and Pop
    • .delete method
    • Addition and Subtraction
    • Union and Intersection
    • Insert
    • Iteration
    • Arrays Rdoc
  • Hashes 22:08
    • Maps and Associative Arrays
    • Created using the constructor
    • Created using a hash literal
    • Stored in a hash table
    • Example
    • Accessing Key-Values
    • Deletion
    • Iteration
    • Hashes Rdoc
  • Ranges 36:40
    • Two dots are inclusive
    • Three dots are exclusive
    • Example
    • Ranges Rdoc

Transcription: Ruby Data Types (Part 2)

Welcome back to Educator.com.0000

Today's lesson will be on Ruby datatypes, the second part.0002

We are going over four Ruby datatypes.0009

The first one will be Boolean.0013

We are going to be looking at true and false.0019

Next, we will take a look at arrays, then hashes, and then something that is pretty custom with Ruby--an object called Range.0023

First, Boolean types.0041

It's very similar to other languages--you have your true/false--but the way Ruby does it is different.0045

First, you have this true value; what I call True is actually using a class called TrueClass.0053

Everything is an object, even the Boolean values.0064

When I call false, it's going to cause the FalseClass, and you have your nil, which causes the NilClass.0071

They all refer to objects.0080

They also have a tendency to lean toward true.0087

What I mean is, for example--first, false is not the same as a zero--so it's not equal to zero.0097

In other languages, you might commonly see zero as a false value; if you pass, in an if statement, a zero, it's going to say it's true.0109

And also, at that same reasoning, the true is not equal to the one value.0121

This is the same as nil; nil is also not equal to zero.0136

We can also look at some of the RDoc to research...see here, nil and false behave the same.0141

Any other value behaves like true.0157

Here is the RDoc for the TrueClass.0166

It says up here it is an instance of class True; also, with the instance methods, if I have an object and I put in true, it can be true or false.0170

The string representation is true, also.0186

If you do a pipe, true, pipe, object, it will return true.0192

Now let's look at the False class; false is an instance of the False class.0201

false & obj returns false.0207

to_s also returns false.0210

There is also a pipe command in here.0214

false|&obj can return true or false, depending on the object itself.0217

It returns false if the object is nil or false, true otherwise.0223

If this said false or some other object, it would return true.0234

If that was nil or false, it would return false, though.0242

That is Boolean types; the next thing we're looking over is arrays.0255

First, they are an ordered collection of objects.0260

I can use any type of object in Ruby, and it can be stored into an array.0264

You can put in String objects, Integers, Fixnum, hashes, symbols...and it doesn't have to be the same type; you can have multiple types in one array.0273

Each array starts at the index 0.0288

Every value that you put in afterwards increments that index by one.0295

So, I put an array of letters here.0303

A, B, C...the first letter in this array will go in the 0 index.0311

B will go in the first index, and C goes in the second--the 2--index value.0330

Now, we are going to show an array of strings.0346

I have Cities, and it's an array that has three values: Los Angeles, San Francisco, and Sacramento.0349

You can call this--it's a method called .empty, and it will return false because that array has elements in it.0359

Arrays can also be created using literals.0376

Example: I have this [Orange, Apple, Pineapple]--this is the correct, proper way to do an array, but I also can use it with the %w%W, and this [Orange, Apple, Pineapple] will also give you this result with the array.0380

Arrays can be created using constructors; not only can I create them using the square brackets, I can use this array.new.0412

This number that you pass in will tell it how much space it should declare for it.0427

This is optional, so I could also just do array.new.0435

When I pass in this value, 3, what it's saying is, "This array is going to have three elements."0445

But I haven't specified what they are yet.0452

So, it's going to create this array, but they are going to be empty; we are going to have nil values in there.0454

The next thing: I can specify them myself.0461

Value 0: I put orange; value 1: I put Apple; and index 2: I put Pineapple.0464

Arrays are accessible by possession and index value.0485

As you've already seen, we put the 0 value as the first value; the last value is actually size minus 1.0490

For this example--Cities has a size of 3, so the last value would be 2.0502

Again,the last Cities, 2, will get us Sacramento.0520

Here are index values, but they can also go negative value, and that is what we are looking at here: negative index values.0537

The first value in this place is -size.0548

That would be our size with the minus, so that's -3.0554

The last value is -1.0562

In this case, you would see -1, -2, and -3.0567

Here are more examples of arrays.0577

For this one, we are going to use the Cities example again.0581

Cities[0] gets us Los Angeles.0586

If I do cities.first, it also gets us Los Angeles.0590

But then, we're going to use negative values.0595

Cities[-1] gets us the last element, which is Sacramento.0598

Cities.last also gets us Sacramento.0603

-2 will go back from Sacramento to San Francisco, the middle element.0608

The next thing we want to look at is the shift and unshift command.0617

With arrays, you will mostly use push and pop, but for shift and unshift--this works on the front of the array.0623

For this example, I have Years [2000, 2001, 2002].0633

When I unshift 1999, this is going to go to the front, so now we have an array with 1999 at the beginning and all the other array elements after that.0637

After I unshift 1998, it's going to push that in the front, too.0656

And then it's going to be in front of 1999, and we have all of the elements there.0664

The shift command will actually pull it out of the first array, so we're going to get back to this element here.0671

It's going to return the 1998 value.0682

The next one we're looking at is the push and pop.0693

This works on the back of an array; this is probably the most common method you have seen in the past.0698

What this does...we have this Numbers=array here, and first we're going to just put two elements in here.0706

So, we're using this less than, less than sign; it's going to put in a 1; less than, less than, equals is going to put in the 2; that's actually pushing the values in it.0718

Another way you can do it is just to use the .push method.0728

Again, you will see it push it to the last element--so it's 1,2,3.0733

And I can also just "pop" that value--notice the array is now 1,2--but this will also actually return the value "3".0739

Next, we're going to look at the delete method.0756

This delete method actually works on any of the array, not just the front.0760

It works on all the values.0773

For this one, you have this array [1,2,3,4,5], and, whatever object you put in here, it's going to look in this array for that object.0775

If it finds it, it's going to delete it.0787

Notice, after I do numbers.delete3, not only did it delete the value, it didn't create a new object.0790

It actually deleted it from this Numbers--so, if I call that Numbers value again, it's just not going to be in that element.0798

Addition and subtraction: here we have two arrays.0810

One with two elements...the other with two elements...notice there is now another array--that [1,2,3,4]--now this is a new array it's creating.0818

It's going to take those two, and with the addition sign, it's going to create a new array with that.0831

Now, if I use the concat function, this is not going to make an array; this is just going to append to the existing one.0838

This existing array, with 1,2, that is actually going to get two new elements to it.0856

And then, for this last part, you can subtract elements, too.0863

This will take the first array and subtract elements in this array.0869

This one is 1 and 3...it finds those...and then it returns a new array, which has 2 in it.0875

Now, let's look at union and intersection.0887

Here we have a union: you have array [1,2,3,4]; you have [2,3,4,5,6]; it creates a new array with all of them in it.0891

I believe you already know this, but how the union works is, you have these two elements; let's say this is A and this is B; and this is going to get elements from both of them.0901

That is why you get this cool thing here.0919

Intersection is different, so, as you see here, it's going to get the same elements that are in both of them.0922

In this case, it gets 2,3,4; again, if this is...we have this example of A; we have this example of B; it's just going to get this part here; and that is the intersection.0929

Next, we're going to look at the insert method in arrays.0951

This one takes two parameters: one is the index of where you want it to push it in.0956

You will see here that it says, "inserts the object before the element with the given index."0967

And here would be your object that you are pushing in that array.0974

For this example, we're going to push 1.5, and we're going to put that on the first index.0979

1.5...we're going to put it right here.0989

After you do that, you do get a 1, 1.5 goes in the second piece, then 2,3,4.0993

Arrays do allow iteration.1009

Here you have a couple of numbers, 1 through 9.1015

We're just calling this H, and we're doing a block that is going to print out these numbers; so we're just going to put 1,2,3,4, all the way to 9.1022

Notice, this num is actually the object that is in the array.1038

You can manipulate it and modify it the way you need to for your code, but that is what num is: it will iterate through each one, one by one.1046

Let's look at the RDoc so we can get a better feel of some of the more advanced things with arrays.1065

Here is our Array RDoc.1086

Here is our new constructor, array.new.1095

Notice that it does allow different arguments: you can pass an array's size and object with it.1101

This is an interesting one: they passed array.new: it takes two elements, but it also declares a hash for each one.1114

You can make each from that empty hash, versus an r; so we just did array.new with 3--default to nil objects.1124

Again, you have your try_convert for implicit conversion.1135

They do include the slice method, too, here.1156

You can--like we used the slice for the strings to get the substring--you can use slices with arrays, also.1160

You can also use this at method; what you do here is to tell it the index value.1181

You say, "Hey, this array, I want to call this method 'at'; I'm going to tell you the index value, and I want it to return the object"--so for this one, it says, "Give me what's at index 0," and it returns me the A string.1188

Another thing you can do is, with this method clear, you can remove all the elements from this array.1205

After I call 'clear', you will see no more objects in there.1211

Another popular one is these collect and map methods.1225

If you run it with an exclamation point, it will do itself, so it will update whatever array you currently have.1231

If you use collect and map, it will create a new object with that array, with the new elements in there.1237

For this example, a.collect, notice they have added an exclamation point; it says a!,b,c,d.1245

But what I really want to show you is the other ways you can iterate.1259

We already went through the each, but another useful thing is this each index method.1267

If I call each index, I can just get the index value back, depending on what I'm coding.1275

So, this value will include 0,1,2, to the array LinkSize, and it will print out the index: it says "0,1,2".1282

Then, you can specify that you want that array object, depending on the index.1296

Again, you can still use empty--if you have an empty array, it will return true--and it has their own 'equal' method.1306

That is the Array RDoc, and you can continue looking at that.1319

Next, we are going to look at hashes.1324

More advanced than arrays, but it allows you to do a lot more cool things, too.1328

Hashes are an unordered collection of key-value pairs.1334

It maintains a set of objects known as keys, and it associates a value with each key.1340

What is nice about Hashes is that they create their own table.1349

So, if you have multiple values, they can be using the same Hash code, and it's a more efficient use of space.1353

They are also known as maps or associative arrays.1364

Let's create a Hash using the constructor.1371

We have our constructor here, hash.new, and these are our keys, so 1 is a key, 2 is a key, and 3 is a key.1375

And we have our values here: value 1, value 2, and value 3.1392

Our Hash now has three keys, three values...and Ruby doesn't make a very nice way, depending on how you code...how you like to use it...to just put key, value, key, value, key, value--just specify it like an array.1398

Here it is: your Hash: you have your key 1, your value 1, your key with 2 string, value, key, and value.1416

This will create the same as this Hash up here, with the constructor hash.new.1427

You can also create Hashes using Hash literals.1439

Here, we have our Numbers Hash; this is another way of the exact same thing we did in the past.1443

Here we have Numbers 1,2,3--that's using our Hash literal.1451

The recommended way to do it is to use symbols.1459

Symbols are only one object; it's not going to create a dynamic new object; it's a more efficient use of space; so this is the recommended way we prefer.1463

Instead of strings, use the symbols as keys.1475

You will notice that this method up here and this one down here--these are both equal.1483

You can do it one way or the other.1492

This one down here is a more popular way of doing it, but this is still including symbols.1497

This will still be 1, 2, and 31504

And again, if you were to call it, it would just be Numbers[1], just like that.1511

They are stored in a Hash table, and they are accessible and modifiable through the Hash method.1524

I actually want to show you this in action--this Hash--because these are actually using the same Hash value.1534

Let's get our terminal opened.1545

I do Numbers=hash.new...I have to get in my IRB.1552

Numbers=hash.new...then I create my 2; here is my key; so that is my value 2; and then I do Numbers, 2, duplicate...1564

Now, we've created these keys and values; let's see what the Hash came up with for us.1583

You see, there is our Hash code; then I can see what the duplicate one is.1593

Again, they are the same.1601

But let's try doing it with some strings.1604

I do Numbers [2] again, this being equal to 2, and with 2_dup, I get 2, also.1607

Let's see if that is the same.1623

We have one over Hash codes...they are still the same.1627

But watch this: if I were to make a=2, b=2, these will be different objects.1631

Not only does the Hash have different keys, it's still referencing the same value.1644

Again, there are some more methods you can do with Hashes that I want to show you--other ways to access the key value.1665

Still with our same example: there is has_key: you can pass in the key--if it has it, it returns true; if not, it returns false.1671

If I run the method keys, it will actually return to me an array with all the keys in it.1684

So, in this hash, there is 1, 2, and 3.1691

I can also get all of the values by calling the values method.1697

It gives me all the values.1699

And you can also use this values_at method; and that says, "Hey, get me a key, and I'll get you the value for it."1703

The value is a 1, and it returns 1.1716

This is...values_at, and this will take a key.1722

The next one we want to look at is the select method.1740

Notice, when I call it, it takes a block with a key and value.1744

What this select method does is, it's like a filter.1751

You can give it the conditions, and, if these conditions hold true, it will return that key and value as part of the Hash.1758

In this case, we're saying, "Give me all the ones with a value that is greater than or equal to 2," so it looks at my Hash and it generates a new one that doesn't have the 1--just 2 and 3.1767

Now let's look at deletion.1785

Deletion is going to be very similar to erase, but instead of objects, it takes the key value.1789

So, for this one, I called delete with the key of 2, and it creates a new Hash with 1 and 3.1797

Now, when I call this delete_if, for this one I have to declare my block as a key value, and for this I say, "I want to delete it if the key value is equal to 3."1810

Look there: the end result is 1.1825

Also, notice that it's still not a new object; I'm still working off of this same Numbers Hash.1829

And, if I want to remove them all, Numbers.clear, and this Numbers.empty will return true.1840

Ignore this...that doesn't belong there.1849

Next, let's look at iteration.1861

The good thing about these Hashes is that a lot of these methods are already in Arrays, so...you learn one, you can learn another one pretty quickly.1863

Let's look at this iteration...we have our Cities...ZIP codes with the Cities...1874

I can use .each again, but unlike the Array, I'm passing two elements.1881

And then, I can do a puts, say, "This value is located at this ZIP code," so for example, for the first one, it would be, "Los Angeles is located at ZIP code 90001."1890

Let's take a look at the RDoc.1924

Let's see what other things we can do with this method.1927

We already saw this type of declaration, using kind of an array, where we're using key-value, key-value, and you can continue creating new Hashes through it.1939

Notice that it's not using new, either; it's a hash, and then square brackets, to declare that.1954

Then, we have our hash.new constructor.1962

This is interesting--the "equal equal"--you can actually check it with other Hashes.1971

It's smart enough to say, "Hey, if they have the same number keys, and the key-value pairs are equal, I can return true for this."1975

We can also look at this example here.1988

h2=h3, it's 1,1,2,2,35,35...notice that we declared it in a different order, but the Hash...there is no order to it; so they're going to be true.1991

Let's see if there are any other interesting things.2012

You can set your default value, so whenever a value is returned, if it didn't exist in the Hash, it will return to that default.2016

Pretty powerful stuff.2029

We saw that delete...there's our iteration...2033

We can also use each_key, so it will iterate through them and do it by key.2036

You can also do each_pair, which is actually the same as each, but some people like using each_pair better; they call the exact same code.2043

You declare the key and value for that.2056

There is a function called flatten; it will take your keys and values, your key-value, key-value, key-value, and it will flatten that to one array.2065

We have it here; you will notice that, in this example they give, they have an array in array--so it will flatten it, and you can set the level of how much you want to flatten it to.2075

Again, you have your has_key, you have include...there are some interesting ones here, like this invert; it allows you to take the Hash and invert it the other way.2091

Your length and size exist for this. 2104

This is an interesting one: you have this method called merge, and you can merge them together.2108

The merge has precedence; so, if they have the same key, the one that is getting merged in will take precedence over the what is already there.2119

And again, they have their 'bang' to merge it to the existing object, or it's going to...without calling it with an exclamation, notice, it just creates a new one.2131

This one is pretty interesting--you need to know--it's this rehash: you want to do this in cases where you have objects that you are using as key--if anything changes in it, it's still using the same Hash table, but something in that object will change; so you need to rehash it, rebuild it.2144

So, they have arrays; they have them declared; and then they call this rehash method.2165

It rebuilds the whole Hash on the current values, but something did change, but the Hash is still using the one that was stored when this key and value was created.2174

That is Hashes.2193

The last one that we want to talk about is Ranges.2197

This one is pretty custom for Ruby.2200

It allows you to get values from start and end, without specifying all of them.2206

The first one we want to look at is inclusive.2213

In this example, all of the elements that have two dots--it's going to include all of those elements and the ones at the end.2217

This will include a range 1 to 10.2229

Now, if we look at the exclusive, which uses three dots, it's not going to include the end elements, so this is just going to be 2 through 9.2235

Notice here, we created a Range, and we asked it, "Hey, for this Range, does it include 10?"2246

That is true if it is inclusive, but on this example, it won't, because it is only 2 through 9.2255

Again, you can use Ranges with a lot of different arguments; you can even...when you create your own classes, you can use them, because they are already built in Ruby.2267

For example, we have this array; instead of typing it all out, I can just do the Range 0..9.2276

And look at that--it will create a whole array with the elements already in it.2284

You can also do that with letters.2289

I do a..z, and it will print out all of the letters for me.2291

Let's take a look at the RDoc.2302

We have our Range object.2309

It uses start and end arguments.2315

And also notice, they do have a conversion method, to_a, to convert it to Array.2321

You still have your constructor, with new.2337

There is an equality method...2341

And you still have your each block.2347

So, it has a lot of the same functionality as Arrays and Hashes, which is nice.2349

There aren't necessarily too many new things with this one.2366

This step function is pretty specific for Range, though.2373

"Iterates over range"...you pass each element to the block...if the range contains a number, it's added for each iteration.2379

You have your to_s, too.2395

That's the Range RDoc.2397

Well, that is Ruby datatypes, the second part.2404

We've gone through Ranges, Hashes, Arrays, Boolean types...2412

This completes the datatypes, but there is still a lot to go over, so I hope to see you for the next Educator.com lesson.2418