Projects in Programming Languages - Ruby, Python, Java | Mashrur Hossain | Skillshare

Playback Speed

  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Projects in Programming Languages - Ruby, Python, Java

teacher avatar Mashrur Hossain, Development Trainer

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

58 Lessons (9h 14m)
    • 1. Introduction to the course

    • 2. Intro to Ruby and structure of the course

    • 3. Setup Integrated Development Environment

    • 4. Getting familiar with online IDE

    • 5. The Command Line interface

    • 6. Intro to Ruby and Hello World!

    • 7. Introducing Strings

    • 8. More Strings!

    • 9. Working with numbers

    • 10. More numbers!

    • 11. Branching - if/else

    • 12. How many fingers game and more branching

    • 13. Arrays in Ruby

    • 14. Arrays continued

    • 15. Hash

    • 16. Ruby style guide

    • 17. Homework - zip code

    • 18. Zip code homework project solution

    • 19. Practice what you have learnt!

    • 20. Build a game - procedural programming

    • 21. Build a game - continue adding features

    • 22. Complete the game

    • 23. Object Oriented Programming - An introduction

    • 24. Inheritance and modules

    • 25. Build a file reader class

    • 26. Build CSV and YML reader classes

    • 27. Automate reading file extensions

    • 28. Project - Word counter

    • 29. Project - Word changer

    • 30. JSON project - read and write setup

    • 31. JSON project - save and complete

    • 32. Preview of final project

    • 33. Check documentation - stock price analyzer 1

    • 34. Stock price analyzer part 2

    • 35. Stock price analyzer part 3

    • 36. Complete stock price analyzer and wrap Ruby section

    • 37. Intro to Java and second section

    • 38. Hello world with Java

    • 39. Loops in Java

    • 40. While loops and more!

    • 41. Working with user input

    • 42. Stack

    • 43. Queue

    • 44. Preview - Mortgage Amortization Schedule

    • 45. Mortgage Amortization Schedule - kickoff

    • 46. Mortgage Amortization Schedule - continued

    • 47. Mortgage Amortization Schedule - complete and save to CSV

    • 48. Introduction to Python

    • 49. Hello world in Python

    • 50. Strings and numbers

    • 51. String formatting

    • 52. Arrays

    • 53. Hashes

    • 54. Branching - if/else

    • 55. Object oriented programming

    • 56. Project 1 - Compound interest calculator

    • 57. Project 2 - Loan amortization schedule calculator

    • 58. Thank you for taking the course!

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.





About This Class

Projects in Programming Languages with Ruby, Java and Python is an in-depth and comprehensive introduction to project based programming using 3 of the most popular and financially rewarding programming languages out there - Ruby, Java and Python. Some of the most popular web app frameworks in the world today like Ruby on Rails, Django, Flask are based on these languages.

This is the course you have been waiting for, a one-stop-shop for everything programming that makes it easy to get started and keeps your attention while you work your way through fun and interesting projects based on real-life problems including Object Oriented Programming!

What's involved?

You'll gain immediate access to: 

- 9.5 hours of crystal clear video content

- FREE live help channel exclusive for students of this course

- 3 free e-books exclusive to this course which walk you through the video content for Ruby, Java and Python 

- Documentation for each line of code in the course using e-books

- Multiple real-life projects like Stock price analyzer and Mortgage Amortization Schedule calculator

You'll find learning both quick and fun!

Are you ready to transform your world and become a confident developer and enter one of the most lucrative fields in the world today? What are you waiting for? Start today!

Who is the target audience?

  • Anyone interested in technology
  • Anyone interested in gaining an understanding of how programming works
  • Anyone interested in entering the technology field as a career change
  • Anyone interested in taking the first step toward a career in development

Meet Your Teacher

Teacher Profile Image

Mashrur Hossain

Development Trainer


Hi, I'm Mashrur, I'm a content creator and development trainer with over 100,000 students worldwide. I specialize in Ruby and Ruby on Rails development and have published over 10 courses in various topics over the last 3 years. I have been a technology professional for over 12 years and have degrees in Computer Science and Economics

In my prior corporate life, I worked with Enterprise Software Systems throughout with roles played in analysis, development and management. I led projects using both agile and waterfall methodologies and am well versed in the inner workings of the software development and delivery world.

During my career, I realized how much I enjoyed training new hires and new team members and helping them succeed. I had dedicated a good amount of time over a ... See full profile

Class Ratings

Expectations Met?
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.


1. Introduction to the course: Hello and welcome to the course. My name is mature and I'll be your instructor for this course. Now I have over 31,000 students at the time of this reporting. And while there have been a lot of successful students of Comotto, my Web app, development courses and some have even formed successful companies out of them, Ah, lot of students come up to me and asked for more in depth knowledge off the programming languages behind some of these flashy frameworks out there like a ruby on rails or Django or flask, right? They want to know about the fundamentals. The language is behind these frameworks, and at the end of the day is the languages which allow you to build fantastic things. And the more you know about them, the better programmer you are. Once you get the fundamentals down, like breaking down a larger problem into smaller pieces and incrementally building programmes, you'll see how easy it becomes. So we'll start with the absolute basics and work our way through ruby and then through Java and then python. We have a large section on Ruby and will focus on object oriented programming, which is essential for any developers. Arsenal. Well, look at a lot of the fundamentals with Java and Python the syntax, you know, as well as build fun projects along the way. But the idea is simple. Grass programming and bed the fundamentals and understand object oriented programming while having fun. Believe projects with the community. There's a free life chat line for this course, where you'll be able to discuss ideas, issues or solutions with other students. They'll be cold challenges, regular support in the Q and A forums and much, much more. So without further delay. Let's get started in the next video, where we discuss the structure of discourse. See there. 2. Intro to Ruby and structure of the course: one. Welcome. So the first part of this course will focus on Ruby. We'll set up our basic understanding of programming languages, data, structures, object or in programming and all the other programming lingo that you hear out there in this section. We'll go in depth will build project after project in a step by step incremental way. The focus of this section is to understand and absorb it'll. No shortcuts will build everything. There will be lots of homework. There'll be lots of projects and study required. Our aim at the end of this section is to be an intermediate programmer. So first, before we get started, go ahead and introduce yourself to the class. Pause this video, go to the Q and A section and introduce yourself something as simple as Hi. My name is mature and I want to learn programming because I want to build things or I want to know how things work. So go ahead, introduce yourself. All right. Welcome back. I hope you're able to do that. And then the second place I want you to introduce yourself isn't the live chat help line and we'll see that in a second. So what is this life chat? Help line. So for this course, in the community of students, there is a free live chat available. Okay, so our students help each other. Sometimes I'm there. Sometimes it will be a ta there. And basically, when you need immediate help, you can post your questions there. Obviously posted in the Q and A here. But if you're not getting a response, you can try there, and you might get help right away. Okay? That's also a great place to meet other students and formed study groups on the like. Okay, so I highly encourage checking this out. So let's take a look below a browser window and go to mature Hussein dot com. Okay, I'll bring you to this site Now. Here, click on live. Help this option right here And from here. There you go. If you are enrolled in the programming languages course, click here to get free. Live. Help. So won't you click there? It will bring you to the screen. Click. Accept, invite. Okay. There you go. And you'll be able to post your questions here. Find users here. If you want to mess it anyone directly, you can just double click on their name and message them. Okay, The second thing I want you to check out is the documentation that's available for this course. So if you go back to Musharraf hossain dot com, and then go to live help for the Java and Python sections, you can click here to go to the documentation. The third thing I want you to look at is to go to Ruby Dash lang dot work, and this is a Rubies site. So go ahead, read through it, get yourself familiar with Ruby. And in the next video, we're gonna get started and set up our development environment, so I'll see you there. 3. Setup Integrated Development Environment: Hello And welcome back this video, we're going to sign up for an integrated development environment using cloud nine so we can write all of our code without any installation off any software. So let's go ahead and pull up a browser window and within it type in C nine dot io And over here, we're going to enter in an email address. If you already have an account with cloud nine, you can simply go ahead and sign in. But what we're going to do is we're going to sign up, so I'm going to type in. There we go. Now, make sure this is an email address that you control because you will get an activation link . Click on sign up, Enter in your name. Going to say your user name is permanent, so choose something timeless and classy. Ok, uh, user with this name already exists, so I'm going to say you to me. Programmer only. Lower case letters. Perfect troubleshooting. Right here. You TV programmer. Okay, some experience. What kind of developer are you? I'm going to say student. Okay, great. How will you use cloud nine coursework next? All right, so confirm your cloud. Nine details. One last check before you create your account. Email name? User name, developer type. Perfect. Next. Apologies for ramen. Course. We need to make sure you're not a robot. All right. Click. I'm not a robot. Great. Next welcome. Okay, start coating. Now, don't forget to set your password from the email we sent to the email address that we used to signed up. All right, so let's go ahead and take care of that. Before we get into workspaces, I'm going to pull up another window and check out my email. All right, So this is what the activation email is going to look like. And there's that activation link. So go ahead and click on that link, And once you click on it, this is the screen that's going to come up over here. I'm going to choose a password. Confirmed the password, confirm new password. Now you consign into Cloud nine. So I'm going to use the email address. There it is. Then the password sign in. Perfect. And there is my workspace right here. I can just close the previous one. So for the different environments you can create new workspace is the only thing to remember is in the free tier of cloud nine. You can only have one private workspace, but you can have multiple public workspaces. And for all of our programming, since we're learning, we're just going to use the public workspaces, which is fine. So create a new workspace. And I'm going to call the first workspace name Ruby on description, Ruby programming and hosted workspace. That's fine. It'll be public. I'm not going to clone anything. I'll choose the Ruby stack right here and then create workspace. And this may take a minute or so. So we're gonna pause this video in return once it's done. All right. Perfect. And here it is. This is what shows up. This is great. Has all of these things going on? I don't want to deal with any of that. I'm just going to say and tour has this read me file. It says welcome to your rails project. Now, we're not going to use rail, so I'm going to skip all of this. It has supporting documentation link. This is very important. But we will also have this in the text document following this video so you can reference it now we're pretty much set up with our development environment. So close out of the Read me. All right. Great. So that's a for the sign of video, and I'll see you in the next video where we explore our workspace. 4. Getting familiar with online IDE: alone. Welcome back in this video, we're going to explore your cloud nine workspace. So first off, if you're confused with the screen, this is what happens when you first log in to cloud nine. If you're already in your workspace, that's fine. I can open that by just clicking here in this open bottom because this is the workspace that I had created. Also, at any time. If you want to get to this screen, you can always type in c nine dot io And if you're logged in, then it will take you here if you're not logged, and it will take you to the sign in page and you can sign in and it will bring you here. Okay, so I'm gonna open my workspace. And I had not updated this at all, So it still looks like my last screen. So if you're wondering where this right this right hand side of the screen came and I had just clicked on collaborate. So if I click on it, it shows up. And cloud nine actually allows you to collaborate with others. That's not something we're going to explore right now. But that's how they showed up. Okay. All right. So here is the welcome screen. The first thing we're going to explore is what this is what shows up here. So here, I'll close this. This area is your text editor. OK, here is where you will write all your code. For example, if I pull up a file, let's say in here I'll just pull this up and there's no need to understand what's going on here. It's just to show that this is an editor. I can type in here, update the file and save it. OK, so all our coding we're going to do in here in this area, I'll close out of there. So what's here? What's on the top left? This is your file directory tree. This will display all of the files and folders that will be working with in our workspace. So right here. This only shows one directory right now, which is Ruby because we called our workspace ruby. And what's important to understand about cloud nine and the weight sets up the ruby stack is that it's only displaying this workspace folder, which we don't want to display. Ruby is actually a rails application here in cloud nine. What we want to display is our home directory, which has a listing off all our directories. So how do we do that? Right here? If you click on it, notice you have all these options, you can refresh file tree. You can collapse folders. You can show up and files root file system. This is what this is displaying. I'm going toe uncheck that. And then I'm going to check this one that says show home in favorites right here. Now, if I click on it, notice how workspace and this was Ruby and live are showing up here. Okay, so how did I do this again? I clicked over here. I unchecked show root file system and I checked Show home in favorites. All right, so that's your file directory. Now, what is this? Right here? This is your terminal. And you can always expand this or minimize that you're gonna expanded by clicking on here. Now you have your terminal occupying the entire screen, or you can minimize it to have a display down here. The terminal is where we're going to issue commands to execute certain things, executes our programs, for example, or well, you know later on, when we work with gets creating files, we can do all of that from the terminal. So what is the relation between the command line and the directory tree? This is basically representing what you have over here but in the terminal. And this is like a visual representation off the files that you have. What am I talking about? See, this says workspace. Notice how there's a directory here folder that says workspace. So I am inside this folder right now. If I type in L s, which means list and again, we will explore these commands later on. But for now, if I type in ls and hit enter, notice how it displays all of these files. Where are these files? These files are in my workspace directory, but I don't see it here. That's because I haven't expanded it. So if I expand this directory, check it out. All of these folders and files are the same ones listed here. You have APP folder. There's the APP folder. There's the BIN folder. There's the BIN folder. Just like that. All of the files and folders listed here are listed here. Okay, so I'm in the workspace directory. And remember in the cloud nine workspace since we chose the Ruby stack workspaces actually a rails application. So we will not do All are working workspace, and we'll see how to move around in our directory tree in the next video. But for now, just so you see, this is basically a visual representation of exactly what's going on here. Okay. All right. So there you go. And just to give you another example, this is my home directory. And within it I have lib and workspace, right? And I'm in my workspace folder. If I want to move up one directory to my home directory, I can type in CD space dot dot Now I'm in my home directory, which is this one. And this one has lib and workspace. So if I type in a less right now, I should see live and work space. So unless enter, check it out. Lib and workspace. OK, so both of them are there. And what if I want to move into this workspace directory and work there again? Then basically, I want to move back here. I can type in CD space workspace Now I'm in workspace directory again. Okay. So again, we're going to explore these commands in the next video. But I just wanted to let you know the relation between the command line and what you see here versus the directory tree. And you can use this to navigate or here as well. All right, that takes care of this. Now, this view that I have this is a theme. And the theme, I think is light. That's why it's showing up with a white background. What if I want to change this? You can change this by going to your preferences. So click on this widget right here. Once you do that, it loads up your preferences and you can see all of these things right here. Code editor configurations, all of this. Okay, user settings, etcetera, etcetera. So let's explore this for a bit. So for Ruby, first thing we're going to do is this soft tabs that has four spaces right here. I'm going to change this to two. I'm going to make it to auto. Detect taps. Eyes on load. That's fine. All this is fine. This is all good finding files. Okay? These are hidden files. We don't have to worry about them for now. Okay? Scrolling down all the way. Perfect. Everything looks good. Now let's move on to user settings. Okay, now here we have factory defaults. We have our user interface. You can explore this as you like or school through to change anything that you want. Okay? And it has settings for finding files. And if you scroll down mawr, there is the terminal settings. Pretty much everything that you need to update or, you know, customize for your screen shows up here. Okay. And over here, I'll scroll down to let's see editors terminal. Then you can scroll down to output, okay? And your code editor and then scroll down pretty much. All the settings for your views are over here that you can customize as you like. I'm going to scroll down to themes over here. It says flat theme, classic themes, syntax, theme, Cloud nine day. Let's try this ambience. Okay? I don't really know. Let's go to classic. Seem I'm going to click on this. Would you like to reset colors to default value? Yes. Here is the classic theme, and I like that. I can change it. I can go to this one. But I like this one. Because with this, when I'm recording video, what I type in really stands out. So I'm going to stick to this theme. So how did I get here again? In preferences, themes, plastic theme. Okay. And once you select something, it automatically takes effect. You have to save anything. You can simply leave it like it is, and I'll close out of preferences again. If you forget where preferences are, right here, Top right click on that widget and it's going to show up. Okay, so we've now explored the workspace again. Check out preferences or move around as you like, just to get yourself familiar with your work space. And that's all I wanted to cover in this video I'll see in the next one. 5. The Command Line interface: Hello. And welcome back in this video, we're going to explore how to use the command line right here. Before we do that, I've changed my mind, and I want to change the theme for my workspace. Okay, so I can click on this, and I can go back to themes, and I can click the flat theme and it goes back to what he was looking like before. Let's give it a shot. If we don't like, it will move back to the prior theme so close out of preferences. Okay, So what is the command line? The command line is this right here. And we can use this to issue lots of commands like PWD ls. And we're going to explore what these commands are right now and what they do to our environment or our files or our directory structure. So let's take a look. I'm going to move this up a little bit right here, so we have more space. So first things first. I have all these commands on top. There's a lot of clutter. What do I do to clear this up? I can simply type in clear hit. Enter. There you go. It just moves to the top. Okay, Perfect. So that's step one. And the command was clear. All right. Next thing I want to do is I want to find out which directory I am in. What is the path off this directory? How do I do that? The command to do that is P W D. Which stands for print Working directory. If you time that in and hit, return or enter. Notice how it says home a bun too. And workspace. So I'm in the workspace directory currently, which is my working directory, which is inside the bone to directory right here, which is inside the home directory. Okay, So if I look over here, there is work space. I'm working here. On top of that, this, which is my home directory, is a 12 Okay, So home of 12 is pretty much where we're going to do all of our work. So how do I move up? One directory to get to this? A bunch of directory. How do I do that? Which is this right? I can do that by typing and seedy space dot dot So I've moved up one directory. How do I move back to workspace directory. I can type in CD space workspace, and I'm back to work space. How do I move back up? Okay. Seedy space, Not There you go. And I would highly recommend that you code along and practice this with me. And there's a reason I'm doing it more than once. We need to build muscle memory in our fingers, so we need to be able to tie these commands out. It's one thing to just see it. It's another thing to actually do it if you see it And don't type along, What's gonna happen is you're gonna think that you remember. But then when the time comes to actually type it out, we'll forget what it is. So we need to build muscle memory. Next, we're going to see what directories are within my home directory again. First, I can type in PWD and C home a bun to this is my home directory. Remember, For all the work that we do in this portion of the course home a bundu is my home directory . So any time you're confused happen pwd, There you go. As long as you see home about your good clear. Okay, so I want to see all the directories and files that are listed in my home directory. How do I do that? I can type in ls hit. Enter ls stands for list. And that will list all the files and directories in my current directory. Now, what if I want to display hidden files as well? Because l s does not display all the files there are. You can only see the files that are listed. You won't be able to see hidden falls to do that. Type in L Espace, Dash a and then you see all of these files that show up. You have your environment settings. You have dog Bashar. See you have your doctor profile. You have all these files. Okay, so now you may be asking. Okay, I see them here. These air hidden files. I don't see them here. How do I see them here? Okay, click on this Would yet right here and click on show hidden files. And you see all of these files and folders show up that are listed here. Okay, Now, this obviously shows a lot of files that we don't really need. We're not going to be working with most of them, so I don't want them to keep showing up here. I can simply click on this and uncheck show hidden files. I don't want to see them, but that's the command to see all the files. Folders, including him and files ls space Doctor. Go ahead, type it out again. Ls space Dash a Okay, Now I want to do the same thing in my workspace directory. So seedy work space. So I'm back in my workspace directory, which is this one, and let's go ahead and type in ls space dash A Notice how the amount of hidden files in this directory are not the same as this. Why is that? Because this is only showing the hidden files and files in this workspace directory. If I click here, show hidden files again, see under workspace. I don't have that many hidden files. There's the get ignore filed. There's the get ignore file. There is the dot C nine folder. There's a doxy nine folder. Okay, great. So I'm going to uncheck this perfect and then tear it up. All right. So we have so far learned how to move to a directory How to display Hit and files, how to list all files and folders, which is good. Now what if I want to change to directories? That wants All right, so let's say pwd. All right, we're at home. 12 workspace. So seedy dot dot Now I'm in home a bun, too. What if I'm one directory up above this? So now I'm in home. Pwd. And this is the home folder. I want to be in my workspace polder, But I'm feeling lazy, and I don't want to type out CD a bun to then again, CD workspace. How can I directly navigate to work space? Well, I think you've guessed it. CD space. A bun to slash workspace hit. Enter. There you go. I have moved to directories. Now I'm back in workspace. All right. See what happened there? I gave it the path, so I gave it the path of the directory to go to. Let's do that again. Cd dot dot Now I'm in a bun. Two cd dot dot Back in home. How do I move back to work? Space CD A bun to slash workspace. Okay, now I type this out, right? What if I'm feeling even lazier and I don't even want to type it out. I know that I have typed us out before. So how can I just retrieve this command? Simply press the up arrow. It shows the last command I typed in President Barrow again. It shows the command I typed in before press it again. It shows the commander Captain, before that, which was pwd right here. And then if I wanna move down, one command from there can press the down arrow and I'm back to this command right here. I can just hit return. There you go. It takes me to workspace directory. Perfect. So now we've learned how to move around the directories, print working directory, list all the contents of a directory and move multiple directories at once. Perfect. Next thing we want to do is we want to create a directory, and we're not going to do that in our workspace, Director, remember, we want to do all our work and home a bun too, right? So seedy space dot dot Now I'm in home a bun too, and I'm going to collapse this so you can kind of see the effects of what we're going to do . Take place here as well. So the command to create a directory. It is M k D I R. That's made directory. Okay, Em que de ir space, and we're going to call it Ruby. Underscore. Projects hit. Enter check out what happened here. There's a directory. It's called Ruby Projects. I tried expanding it. Nothing happened because I don't have any files in it. What am I in my Ruby Projects folder just by typing in NK de ir Ruby projects? No, I'm still in my home. A bun to folder type and twd still in home. Open toe. I simply created Ruby projects. I have not navigated to Ruby projects. How do I navigate to Ruby projects? CD space. Ruby underscore projects. There you go now in Ruby Projects. Let me clear this up. Now, if I'd have been PWD home a bun to Ruby Projects here. Okay, let's type in ls shows nothing, because I have no files and ruby projects. Let's create a file from this user interface first. All right. Right. Click on the Ruby Projects folder, new file, and I'm just simply going to call it test dot R B. All right, So here's this file I created from the user interface. Our director tree right here. Now, if I type in unless he had enter notice how test dot r b shows up, Which is this one? Great. Now what if I want to create another file, But instead of doing it from here, I want to create it from the command line. Simply type in touch. Test one, Let's call it test one dot R b hit. Enter notice. It didn't show up automatically here. Sometimes this may happen. How do you make the show up? Click here. Refresh file Tree. There you go. Test one that dot RB showing up. How do I make sure that it showing up here At last? There is test one dot R b. Now, how do I rename a file? Let's I want to call this mature dot R b. So renaming a file I have to say m v for move and I'll give it the file that I want to rename, which is test one dot r b space. Then I want to give it the file name that I wanted to be so mature dot r B Now if I type in ls notice how? Test one dot r b is gone. Now it's mushroomed out RB and test RV. And over here, refresh file tree. There is my sure dot R b. So go ahead, execute this command again as you're following along and make test start RB your name dot RB and I'll wait. Okay. I hope you manage that. How would I do it? M v test dot R b student one dot r b it enter unless now I have matured out. RB and student juan dot r b. Okay, so now we've seen how to rename a file. What if I want to copy a file into a new file So both of these files are empty, so that wouldn't really do anything. Right? So first, let me pull up one of these spots. I want to pull this one up, and I'm going to simply say test, okay? Command us to save it. Now, if you're in windows, you can say control s or you can simply say file and then save over here. But, you know, shortcuts will always help. Now, I have some content in this file. All right, I'll close out of there. What if I want to copy the contents off? Mature dot r b create a new file with the same content Abdu Cp Space assured at RB space and then the new file name. I'll say mature new dot r b. Okay. Now if I'd have in a less notice how now I have a mushroom do not RB file, But I also have them assured RB file because I didn't rename it. I simply copy the contents. Now, if I refresh file tree, it shows up. If I double click it, there is test because it was a copy. Okay, so I'm going to close out of here. Okay, so we've made a lot of progress. You learn how to rename file copy file, create new directory. Always know pwd To find out which directory you are in, you have learned how to use their less. You've learned how to use their less dash A. You've learned a lot of things already. Now let's talk about how to remove something. Okay, so clear this up. I want to delete this student one dot RB file. How am I going to do that? So I can say RM space student one dot r b Okay, type in ls student Wonder RB is gone here Refresh file tree Great. Now what if I want to remove a directory? Let's create a test directly first. M k d I r test underscore directory. All right, But I've been ls there's test directory. I refresh file tree. There's test directory within Ruby Project because I created this within my Ruby Projects directory. I want to delete test directory So what if what happens if I type in RM? Which is the command I used for student one so RM test underscore Directory cannot remove test directory. It is a directory. So this is for files, not for directories. So how am I going to do this? Rmd ir test underscore. Directory it. Enter. There you go. It worked at less does directory is gone. Okay, now let's say I want to delete the Ruby Projects Directory and there is something interesting here. First I'm going to refresh file Tree Ruby projects is not an empty directory. Ruby Projects has content within it mushroomed out RB and mushroom new darby. So what's going to happen when I try to delete Ruby projects. First of all, I'm in Ruby projects. I'm going to move up one directory, CD, space dot dot Now if I'd happen ls there is Ruby project, so type in RMG ir ruby underscore projects. What happened? Failed to remove Ruby projects. Directory is not empty. So whenever you have content within a directory, you cannot delete it by using RM d i r. Now there is a dangerous command that you can use right now to delete directories which are not empty. I would recommend avoiding this as much as possible, but sometimes it's necessary. So we're going to learn how to do it. RM space dash R f What does this stand for? Recursive force. Okay, so remove with force. Let's see what happens when we do this. Are you be why underscore projects enter. There it is. Ruby Project is gone. Unless Ruby projects is gone again, I recommend avoiding this as much as possible. But now you know how to do it. So here's the same. What if I want to not only create Ruby projects, but I want to create another folder within Ruby projects? How do I do that? Em que de ir ruby underscore projects. And let's say I want not only to have Ruby projects, but I want Ruby projects to have another directory within it called Test Directory. So slash test underscore directory hit. Enter cannot create directory. Ruby projects test directory. No such file or directory. So I was not able to do that. Meaning mjd ir did not allow me to create test directory because it couldn't even find Ruby projects into the directory. So let's go ahead and create And Ruby Projects M k D I r. The Ruby project. So now I have Ruby projects. Now, since I'm feeling lazy, I don't want to navigate to Ruby projects and create test directory. I want to do it from this directory itself. How do I do that? I can do em. Que de ir ruby underscore. Projects slash test directory here. Enter. Okay. Now, if I refresh file tree, check this out. Ruby Projects has a test directory within it. Now I'm feeling a little annoyed because notice how when I tried to create em que de ir ruby projects test directory. It didn't allow me to create this because it couldn't find Ruby project directory. Okay, so I wanted to create this ruby projects directory, even if it doesn't exist. So first, let's remove these directories. Right. So RM dash R f ruby underscore projects that got rid of ruby projects. Okay, if I type in l s, I don't have a Ruby project will clear this up. All right, So what I want to do is I want to create a Ruby Projects directory and within it, have a test directory, and I want to do it without issuing multiple commands. M k D I r Space dash p. And then I'm going to say Ruby, underscore. Projects slash test underscore project he had enter. Okay. What happened there? This time? It worked. Ruby projects that created within Ruby projects. There is test project Perfect. So we've learned this shortcut. Now I want to navigate to Ruby Projects CD ruby underscore projects. And this is where we're going to create files and start working with them OK, in this Ruby Projects folder for our ruby section. So I want you to remember this path P w d home a bun to Ruby projects now as ah, homework exercise, I want you to write out all of the commands that we've learned in this video and posted to discussions, not just write them out right out a short one line summary as to what that command does right out command. So let's say ls with a dash and say this lists all files and folders in the current working directory. Okay, so just like that, I want you to write out all of the commands that we explored in this video and then posted to discussions. If you're using the current student experience, you can simply click on ads discussion and then give it a title and say listing commands and list them out. Or if you're using the new student experience, simply click on the after question, give it a title and say, listing commands and then print them out. Okay, And with that, we conclude this video on using the command line interface. I'm looking forward to seeing the commands and the one line responses that you post good luck and also you in the next video 6. Intro to Ruby and Hello World!: alone. Welcome back in this video, we're going to learn about programming languages and Ruby. So what is a programming language? Let's look up the official definition from Wikipedia right here. I have typed out what is a programming language in Google. This is what I brought up. Programming language is, ah, formal, constructed language designed to communicate instructions to a machine, particularly a computer. So, as you can see, programming can apply to virtually any machine. But when we're referring to programming languages most of the time we're referring to communicating with your computer. So what are programming languages? Ruby is a programming language. Then you have Java C plus plus C sharp python. You have lots and lots of programming languages that have arrived over the years. So what is Ruby? Ruby is an object oriented programming language, and here's the best part about Ruby. Ruby is designed with programmer happiness in mind, and you will see it Mawr and Mawr as we explore working with Ruby. So to learn more about Ruby, go ahead and go to www dot ruby dash lang dot org's. And here you can find all kinds of information on Ruby latest releases you can even download Ruby from here, which we won't need because our immigrant development environment comes with Ruby. And here you can even try Ruby in your browser to give it a go. But again, we're going to skip that, because we're going to do it in our own way. So who created Ruby? Ruby was created by this guy right here. His name is Mats, as he's known in the Ruby community, but his real name is Yukihiro Matsumoto. So here's your first homework assignment for this course. Read up on Yukihiro Matsumoto. So you get familiar with him. And the aim of this exercise is by the end of this exercise, I want you to be able to refer to him as Mats instead off by his full name. Okay, we're learning about this language, and this guy created it, right? So we need to know about him. Okay, so Ruby has been around for around 20 years, but it really exploded once Ruby on rails came on the scene and started being adopted as the Web about framework for many off the popular web applications that you see nowadays. So ruby on rails is very powerful but Ruby on rails, you know, being as powerful as it is, the underlying language behind it is Ruby. So it's very important to get really good at Ruby. If you're trying to be very good at rails, they kind of go hand in hand. A lot of people have started with Ruby, then moved onto rails or started with rails to build Web applications and then gone back to improve their ruby skills as the need arose. So feel free to browse the site. Look at documentation. Look up things to do with Ruby. As much familiarity as you can. In our course right now, we're going to start programming in Ruby right away. So back to our I D. E. Here we are. There's my Ruby Projects folder that we created in the last video within it. I have test project. I don't like this Test Project folder. It is unnecessary. I'm going to remove it. So first step ensure you are in your Ruby Projects folder right here. Our Ruby Projects directory in your command line and type in our M. D. I. R. Test project. There you go. It's removed. So first thing we're going to say, as is the norm with all programming language. Introductions were going to say hello world in Ruby. So first thing, we're going to create a ruby file. And how do we create it over here? You can either go right here in your movie projects folder, right click and new file. But we are going to use the command line. We're going to say touch. Hello, world dot r b. So this dot RB extension is what identifies this file as a ruby file. So it return. There you go. There's my file and God created I'm gonna pull it up. All right? So what do we want? Hello world to do? We want Hello, world to say hello world when we run this program So go ahead and type in puts and within quotes Hello, world And then save it Command s or control s if you're using windows So what is this doing saying Puts alot world What we're essentially saying is put and then s is short for string . Hello world Simply print out hello world on the screen when we run this program So let's run this program to run a ruby program You have to say, Make sure you're in the directory, which contains the file, which we are. We're in Ruby projects and there's our file. We're going to say Ruby Space and the name off the file. Full name of the file, including the extension. So Ruby, Hello world dot RB return. There you go. It says hello, world. Perfect. Now you may say, Oh, you said this is supposed to be an easy language. Why are we saying puts? Why can't we just say print Hello world since its printing Hello world to the screen. All right, let's try it. Print within quotes. Hello. World Command us to save. Run it again. I can simply press the up arrow to load the previous command and then hit Return. There you go. Hello, world. Hello world again But what happened after the second Hello world This prompted showing up right after that and that is the difference between print and puts print does not add a new line. It simply prints what you give it to the line and then starts right after that puts on the other hand, adds a new line after it. Okay, so what if I do puts here and print here. Can you take a guess what's gonna happen? Okay. I hope you've guessed. Right. Let's try it out. There you go. After the first print Hello, World. It executed the next command right away, which is puts hello world Right after it without adding a new line. And then after the puts it got to the next line. Okay, so that's the difference in print and puts. So I'm going to just remove this print because I, like, puts I like the new line, so save that. All right, so that was the simplest way off. Riding out. Hello, World in Ruby. We're going to explore to other ways of doing this. First, I'm going to introduce you to what is variable. So let's create a new file. We're going to call it first Touch, and then the file name is going to be Hello, world variable dot r b. Okay, so there it is. Hello, world variable. That RB file. Let's pull it up. We want this to say hello world as well, but we want to assign hello world to what is called a variable, something we can change. And then we simply want to put the variable name. What am I talking about? Let's check it out. Hello? We're gonna call are variable Hello and then we're going to assign it to something. So we're going to assign hello world to our variable. Hello. So hello is referencing appointing memory in our workspace that says Hello world. So when I refer to Hello, it's going to point to the same reference in memory and pull up Hello world What am I talking about? Check it out. I'm simply going to say puts Hello here I do not have to write out hello world because I've already assigned it to this variable Talkin simply puts the variable Let's see what that does. Save that Let's run Hello world But now here I have to say hello world variable since it's a different file not RB There you go. It says hello world So why do it this way? Instead of that? What we can do is we can simply assign the variable to something else and then put out the variable again. What if I say hello is assigned to my name is mush Roar? Save that and I'll leave the rest as it is Run it again. It prints out My name is mature. Perfect. Now what if I want to reassign what hello is assigned to and then put it out again? Let's do that. So I'm going to reassign hello to hello World on. Then I'm going to print it out again. So let's see what that does right here. Check it out first Hello is designed to My name is mature and then I'm printing it out. So my name is mature. Then I'm reassigning hello to hello world and then I'm printing it out again And then the next time it's going to say a little world cause now it's pointing to Hello world Perfect. So I'm gonna clear up the screen. So now we have explored how to say simply Hello, world And then how to use a variable to say hello, world. Now the last thing we're going to explore in this video is how to use a method to say hello world. So I'm going to create a new file going to call it Hello world method dot r b And there it is. I'm gonna pull it up. How do you define ah method in Ruby. What you need is this keyword d e f that stands for define and then you need to give your method a name. So I'm going to give my method name off. Hello. And since you started this method, you need something to end this method And how do you end it? You simply say end. There is a method. I have an empty method here. If I run so ruby. Hello world method, not RB. It does nothing because I have an empty method. This method does nothing. All right, so let's make our methods say hello world so puts Hello, world exclamation mark Save that So our method says hello world, right? So let's run our file again and see what happens. Nothing again. What happened? It's supposed to say hello world. Well, a method says hello world, but I have not called this method. So that's the important thing about methods. Methods need to be called in order to execute what's going on within them. So I'm going to call the method. How do you hold a method again? Simple language. All I'm going to say is, Hello? Save that. There you go for Low World So after defining the method, I call the method and it worked. What happens if I delete this? If I called a method first, right and then define it, then what's gonna happen? Let's try it out. Nothing happens because when I'm calling it, there is no method definition. It couldn't find the method. Hello. So something important here. You need to define the method first, and then you can call it. Okay, Try it again. Make sure it works. All right. Perfect. So now we've explored three ways off doing this. What's the most important? What's the most important to remember is this method, because check it out over here. If I need to say something else, I have to keep writing outputs. This puts that etcetera, etcetera, over here in a variable. I need to reassign the variable every time and then print it out, and then it's going to say different things. But with this one, the advantage is with methods I can pass in arguments, and then it prints out arguments. That way, I can basically call the method with any argument I want, and it will print it out. Or it will do whatever the method calls for it to do. So. What am I talking about? Let's say within parentheses, I'm going to say anything. Okay? So anything is the argument that I will be passing to this method and then over here instead of saying, puts a little world I'm going to say, puts anything because that is the argument that got passed in. So now if I have to call this method, I have to call it with something with an argument. So how do you pass in an argument to a method and call that method in Ruby? You do it by calling the method and then within parentheses. Give it on argument, anything But here. I want to give it an argument that I actually wanted to display. So I'm going to give it the string. Hello, world. Okay. And the string, if you're wondering what a string is because I have made several references to it. String is whatever you put within double quotes. Okay, so that's a string. It's a collection off characters. All right, so save that. Let's were on it. There you go. Hello, World. Okay, You might say all right. We were able to do this without all this complication before. So why do we need this? All right, let's try something else. Now I can say hello. My name is mature. Notice how now I'm calling this method with a different argument. So let's do that. Hello, world. My name is mature. It caused this method with two different arguments in two lines and it executes both. Perfect. This is the power off methods. I can call it with different things and have it execute common logic. All right, now, the third thing I'm going to do, I'm going to say hello. Too much typing. All right, Save that. Let's run it. Perfect. Printed out, All three. All right. Now, what is the point of this last thing I wrote? Here's the beauty of Ruby. The idea is to make it look like English, right? But these parentheses, they make it look too much like programming. That was not the point. So let's do this. Let's get real lover underseas. Get rid of that. Get rid of that. Get rid of this as well. Save that. Let's run it again. Whoops. That was clear. Wrong command. There you go. What What happened? It worked all three of them printed out. So that's the beauty of Ruby. You don't even need to give it the parentheses when you're passing in an argument the method executes. It knows that this is an argument for this method. So look at how this looks. Hello. Hello, World. Hello. My name is mature. Hello, Too much typing, reading Just like English. And there it is. It's friending them out. And now we have explored three ways off saying Hello World in Ruby. Perfect. And that's all I wanted to cover in this video. And remember how I kept referring to strings? Well, that's what we're going to explore next, So I'll see you in the next video. 7. Introducing Strings: Hello and welcome back in this video. We're going to talk about strings, and we briefly talked about strings in the last video. We're going to explore it in more detail. But before we do that, now that we know how to say hello world, we need to know a little bit. Maura about Ruby, at least to check what version off Ruby were running. So how do you do that? You can do that in the command line by simply typing in Ruby Space Dash V for version. There you go. It's saying Ruby to dock tree that, Oh, that's the version we have installed. And don't worry if this is something else for you. If it's too dark to or or depending on when you're using the scores, if it's higher, it does not matter. It's OK. It's OK to go as long as it's over to 00.0 dot oh, you should be fine. So something else to know about Ruby is you could have multiple versions installed and used along that You like. We're not going to explore that in full detail here, but your Cloud nine environment comes with a version manager known as R. V M. It stands for Ruby version manager, So let's type that out. R V M and then space I will say list hit. Enter it says, are VM Ruby's. You can see we have everything from 1.9 dot 320105 to go to four and 2.3 dot oh installed. And it also says which one is the default? Which one is the current? And you can play around with this and switch between versions of Ruby. Let's take a look. Let's say we want to use to dot juan 0.0.5. How do we do that? Our VM space use space 2.1 dot five There you go using 2.1 dot five now, if I have in Ruby Space Dash V the version, it's using its to die $1 5 What? We don't want to do that. We want to use the latest one. So let's go back to our default R V m used to dock tree 10.0, now we're back to using ruby to dot tweet Auto. Ruby spaced Ashby. There you go. If I type in R V M list, there you go. If you want to install Ruby, you can give it our VM install and then the version number and basically there. Lots of things you could google this Oregon Look up, RV, EMS documentation. Do you see how you can play around with different versions Were not going to explore that right now, so I'm just simply going to clear this up now. There are other version managers. I personally like R V m. And I use that and our cloud nine environment comes with it, So we're just going to stick to our VM. So what is a string? Let me create a new file here. I want to say new file and there's an Ruby Projects. I'm going to call this playground dot RB. Let's open it up. So, what is the strength? I'm going to put this special character over here, pound, and then type out what is a strength and what is what happened here. What is this? This is a pound character. Dis implies that everything you right after this in this line is a comment. OK, so I'm going to save that if I just run this ruby playground dot RB Nothing happens because This is a comment Ruby basically understands is a comment and ignores it and moves on. So what is the string? Ah, string is a group off characters in a specific sequence. And how is it specified? It specified by putting it within quotes like this. Double quotes, single quotes, both our string. So let's say hello, world. All right, this is a string. Then I'll say good morning. This is a strength than within single quotes. I'll say, How are you? This is also a string. We have three strings here. And we saw this when we're playing around with the low world. Right? Right here. If you saw here, we put hello world within string. And then in the method we saw these three strings that we were using. All right, so that is how you define a strength. It is a group of characters in a specific sequence, and it specified by putting it within quotes. All right, so let's run our program. Nothing happens. We want to print this out. What's hello world? Save it, Ron. EDS. Okay. Now for the 2nd 1 I'm going to say greeting equals good morning and greeting. I'm assigning as a variable, and then I will simply say, puts greeting. Save that we're on this. Okay, a little world. Good morning. Perfect. So, what are we doing here? We're basically writing something that we're saving the file. Then we're running the program. Doesn't sound very efficient because we're repeating this cycle, and all I'm doing is learning are playing around with it. Right? So Ruby actually gives you an interactive playground, and it's called I R B for interactive Ruby. And you can access it simply by typing. And I RB and hit. Enter. This is also known as the Ruby Consul. Okay, so let's see what happened here and just do that over here. I'll just say hello within quotes returns. Hello. Okay, let's say greeting equals Good morning. Good morning. And if I type in greeting, it says Good morning. If I type inputs, greeting says good morning and it returned something. It returns to Neil. Why did it return new? I just said, puts greeting, And it printed out the value off greeting, but it also returned. Now here is the thing. Every method in Ruby returns something here. I was simply referencing the variable, so it displayed the value of the variable, but here actually ran a method puts puts is actually a method. So I ran the method and puts returns nil. So it printed out the value of greeting. Then it returned. Now all right, so that's what it is. So interactive. Ruby. Notice how I'm at the bottom here and I don't want to be at the bottom of the screen because I'm in the terminal and I'm going to be playing around in the terminal. So how do I maximize this view? Right here on the right. Over here. Click on this. There you go. It's maximize. Okay, so now how do I minimize the screen? I'm going to click on this. And currently there is a bit off a bug over here. In this view, in this new theme for Cloud nine shows up at the bottom. And if I click on at sea, I can't minimize or maximize this are you know, it's it's on the bottom. How do I fix this? If you scroll up here right here, If I click here, There you go. It's back. And by the time you used is this may be fixed, but This is only an issue with this team. It doesn't show up in the other one. Now. The second part, I'm going to move back to maximizing the screen. Notice I'm still at the bottom. How do I clear this up? I clear this up by typing in control. L There you go. Now it's clear now at the top of the screen. Perfect. Let's continue our work with strings. So let's assign first name equals Mature. What happened? Check this out. I have first name equals mature and nothing happened. And this is a troubleshooting method that you're going to need to know. What happened is I forgot the closing quote over here. So I r B does not know that I am done with typing. So I'll have to close, put the closing quote and here enter, and then it displays it. What happened here? Slash And that's a new line. Okay, we're going to come back to that. Let's try this again. First name. Close quote. There it is. Mature. Now if I type in first name mature last name going to say Hussein there is the same Now what if I want to print out first? name and last name in the same line. First name plus last name. What happened here? Plus sign for variables or for Mature and Hussein. This is not addition. This is called string concatenation. Okay, You're basically adding the second variable after the 1st 1 complete. So let's see what this does. Well, sure her saying It gives me one string but there's no gap in in the middle. How do I put a space in the middle? We do that by typing in first name plus and then empty strings. So I put in a quote space closed a quote space, plus last name. Now I have more sure of saying Okay, I can do the same thing Puts first name plus empty string plus last name. There you go, Mature Hussein. And in return, nil as puts always returns nail. Now how do I display first name and last name in a sentence? I want to say my name is my sure saying okay so I can do this I can say puts My name is my sure are saying right but I already have the values off mature, stored in first name and Hussein stored in last name, so I can reference the variables. So let's try that puts My name is first name. Last name. Well, what happened there? It just printed out. First name, last name because this is part of the string. It's not interpreting it as anything you know that has value. Are reading it as a valuable is just seeing it a string. So how do I make Ruby interpret this and this as variables with values and print out the value? How do I do that? That's called string interpolation. Okay. And we do this like this puts my name is and when you want to display the value with string interpolation pound open curly brace. First name close curly brace, then pound open curly brace. Last name close Carly brace within. Quotes it enter. My name is mature Hussein. There you go. So that is called string interpolation. Now what if I do this using single quotes? So pull up the last command. I'll replace the double quotes with single quotes. Here. Enter Whoops. It didn't work. Why did it work? Because string interpolation Onley works with double quotes. It does not work with single quotes. So it did not evaluate the values off. First name and last name. Okay, Now let's do something else Over here. Let me get rid of puts and enter. Okay, now what happened here? Check this out. My name is slash first name slash Last name. Why is this slash showing up? This slash is showing up because it's saying it escaped to this. Okay, this is a special character. When you put a backslash like this before something it says escape the special character afterwards. So it's not evaluating this. So what if I wanted to type in something like this? Let's say my friend asked, How are you? Okay. Hit. Enter. See how it shows up nicely with in quotes because I'm quoting someone. But what if I did single quotes? My friend asked, How are you? Within single quotes? I have syntax. Error. Okay. So control l to clear this up. So how do I display that? If I don't want to use string interpolation, I can escape it. So just here, Over here I will say escape this. And right before this one, I will put another slash and say escape that so escape. This single quote escaped that single quote. So escaped the special character here and then escaped a special character there. And now it prints it out. Perfect. Now let's look at some methods. So we have first name, right? How do I know what class is? First name. So we know that it's string. But how do we know that? Let's type it out. So class off This is string. So there's a string class, right? So first underscore name simply type in dot class. There you go. It's a string. What if I have five? The number five dark class. Do you think you LeValley to strength? I don't think so. It's gonna say fixed, numb. Since it's a number, this is not a string, I'll say within quotes. Hello world dot class String within quotes The number five dark class string Because I put this within quotes, it's no longer a number it seeing it as a strength. So how do I know which methods are available for me to use on this variable on my string? Let's try first name dot methods and there you go. So many methods are available. Check this out. I can type in first name. Let's pick one right here. Inspect dot Inspect. Check it out. It's asthma. Sure. First name dot mil. Question mark. False because it's not know. First name dot Empty question, mark. Nope. It is not empty. How about first name dot Length? Length is seven. Because my sure has seven characters bought. What if I say first name equals mushroom or space H. It should have eight characters, right? If I try First name dot length says nine. Why's there? Nine? Because there is a space and space is counted by length. Okay, empty space is counted by length. So what if I wanted to reverse the value off? First name can simply say first name dot reverse. And I want you to remember these two methods because you're going to need this for your exercise dot Reverse and not length. Okay. When you clear on the screen, What if I want to find out if a string is empty? Okay, double quotes dot Nil false, but it looks empty to me, but nearly evaluates this toe false because it's not nil nil dot Nil will evaluate the true . So what if I want to see if it's empty like this? This I want to read as empty for that, I can use the empty methods of this dot Empty hopes. Question mark. True, There we go, so you can use empty in that situation. Now let's cover something interesting about strings and variable and what they point to, Let's say I'm gonna say first name equals mature and then I'm going to say new first name equals first name that also says mature. So if I just do new first name is going to say my sure, But here is something tricky. First name is pointing to a reference in memory. That's asthma. Sure. Okay, new first name is not referring to first name When you say new first name equals first name new first name is also pointing to that location in memory that says mature. Okay. And how do we test this out if I change first name, too, not mushroom or now First name is not a sure, but if I type in new first name, which remember I had assigned as equal the first name, this is still going to say my sure, because this is still pointing to that space and memory that says mature first name now is pointing not to that space, but new first name is still pointing to that space. Okay, so that's something important to remember. Okay, Now I'm going to minimize the screen. And again, remember, this is causing an issue, so I'm gonna have to There you go. I'm gonna move this down, okay? I hope they figure this out is probably a good idea to use another one of the themes because this one is the only one that's having this issue. So I'm going to say control. It'll to clear this up. And so how do I exit from the terminal C? I'm still in the terminal. How do I exit from it? Simply exit. There you go. You're outside. I'm sorry. I said terminal, I meant the i r B console, so you can simply time an exit to exit the council. All right, so now we've covered a very good amount on strings, and we're going to continue this in the next video. Okay, I'll see you there. 8. More Strings!: alone. Welcome back this video, we're going to continue working with strings. Where would lift off? We had looked at how to use the I R B console and also how to create programs here, save them and run them using the terminal. So next step. How do we get user input in the program? For example, What if I want to prompt the user to tell me their first name? How do we do that? So this is how we do it. First, we need to ask the question. So puts what is your first name? Question? Mark. If I run this ruby playground dot R b, it's going to prompt me. What is your first name now? I need a way for the user to type in what their first name is. So how do we do that? First, I'm going to assign Ah, variable. Okay, that's suited to what I'm looking for. So in this case, first underscore name and I'm going to set it equals to the input that the user puts in. And how do I get that input? I get that using this method. Actually, it's a combo of two methods, but this is what it looks like gets docked Trump. See that? So let's run it and see what happens. What is your first name and see? There's the prompt. It's not going to do anything with it, but at least it's prompting me for my first name. I hit Enter and there you go. Nothing happened. So let's print it out. So puts first name. Save that. Let's run it again. What is your first name? Sure. There you go. It prints it out. So let's make this say something better. Will say Welcome to the program. And then I will display first names value. So pound open. Curly brace first underscore. Name Close. Carly Brace. Close quote. Save that. Let's run it again. Clear this up first. What is your first name? Assure. Welcome to the program. Assure. Okay, Perfect. What if I want to make this something bigger? For example, I want to prompt the user for their last name. Also So puts what is your last name? Question Mark. Then last name equals gets chomp. And then I will say put welcome to the program. First name and last name. So pound over, Curly brace. First name pound open curly brace. Last name Close, Curly Brace. Save that. Let's run it. What is your first name? Mature. What is your last name of saying? Welcome to the program? Was sure We're saying perfect. So that's how you get input from the user. Clear this up. So that brings us to your exercise, and we're going to have to exercises. Okay, so let's take a look. The first program will run something like this. I'm going to show you how it runs will clear this up. So ruby e x one dot r b You're going to select a song that you like and you're going to leave to space is empty for the user to fill out. So I've selected Welcome to the jungle. We have dash and dash. So first question, what should be the first word I'm gonna say fun? What should be the second word going to say games? Welcome to the jungle. We have fun and games. There you go. It completes it for you. So that is your first exercise. Okay, so just like we did here, you get input from the user and then you display it. Okay. Now, once you have completed it, copy the code for your program and then paste it in the discussions. If you're using the new user experience, click on Ask Question and then put a title and say strings exercise, Exercise one and then paste the code. If you're using the current you Timmy experience than at a discussion, put a title that says Strings Exercise one and then paste the code. All right now on to exercise to is going to run like this. Let's run it ruby e X two dot r B. Welcome to the jungle We have dash and dash. What should be the first word going to say? I'll just say something else. I'll say hello. I want you to be the second I'll say world and it should display the complete adverse is welcome to the jungle we have Hello and world. So whatever you prompt the user to input that should display. Then your song has 46 characters in its including spaces. So remember you're gonna need to calculate this. Here's a hint. You can use the length method. All right now, The last part, the song reversed sounds funny, but here it is, and then it displays the song in reverse. So the entire part. Welcome to the jungle. We have, whatever the user inputs and whatever else the user inputs. But the whole thing reversed, all right, And the same thing After you have completed this, copy the code at a discussion or question. Name the title strings exercise, too, and then paste the code our rights. And I look forward to seeing your code. And with that, we conclude this video on strings I'll see in the next video. 9. Working with numbers: Hello. And welcome back in this video, we're going to start working with numbers. So far, we've explored strings. So let's go ahead and get started in the terminal. Click on this button to go to the big screen for I R. B. Because that's where we're going to use right now. 12 Clear this up first and then type it. I r b. There you go. We've loaded the playground. So what are we talking about? With numbers? Go ahead and type this out. One plus two. Check this out. It gives you three. What happened here. How did Ruby No, to add these two. It's because we gave it the plus Operator and D's were interpreted by Ruby as numbers as fixed numbs. Okay, so let's verify that one, doc class there it is, fixed, numb. And if you remember, if I put one within strings, Doc class is going to say string. So will it work for one within quotes, plus to within quotes. Hit. Enter. It gives you one plus two, which is string concatenation. It doesn't perform integer arithmetic on it. All right, so you get 12 which is not what we're looking for. in this case, but that's what it gives you. All right, So let's try multiplication. So five times 10 50. All right, How about Division 20? Divided by five. Four. How about 20? Divided by five without spaces still gives you four, so it interprets it correctly now. 20. Divided by five plus four. Eight. Correct. How about five plus four divided by 20 five? What happened there? Nine. Divided by 20 is not five. So what happened here? It divided four by 20 and then it added five to it. Now four. Divided by 20. What is that for? Divided by 20 zero? That's not right. 45 by 20 is not zero. But that's what this expression evaluated too. So it's simply added that 0 to 5 to get five. And that's what we got here. Now what if I do 20 divided by six. I got three. That is not correct because six times three is 18 not 20. So what is happening here? What we're doing here if integer arithmetic. Okay, so it's leaving out the decimal points. So what if we want the digits after the decimal? So what if we want accurate results, then we're gonna need floats. So 20 divided by six and we want this to be afloat. How do we convert that dot to underscore f, which is docked to float. There you go. 3.33333 That is the correct result. So let's look at another way of doing this Doc to F is a method. How do we use a shortcut in, say, 20.0 by six? What's that? Gave us the correct answer as well. So you can add a dod zero and it will interpret that as float and assume that you want the answer to be afloat. How about 20? Divide by six point. Oh, that works as well. Okay, now what if we do six foreign Oh, docked to I doc to f is to float Doc to I is to integer notice how it gets rid off the 0.0 . So what if we do 20 divided by 6.0 dot to I It gives you three because we're saying converted to integer An integer cannot have decimals. Our rights control l to clear this up. Let's look at the mod operator. Mod, as you know, stands for Modelo, and that helps you find the remainder. So 20 mod operator symbol is percentage. So 20% 3 to now. What is this too? That's the remainder you get when you divide 20 by three. Okay, so the remainder is too. So let's try 18 mod five, three. Ok, so 18 divided by five. The remainder is three. So there you go. Now, if you've done some programming before, you probably already know this. But any odd number model of two will always give you one. So 21 mod two will give you one about 23 mod two will give you one. So any odd number lot of two will give you one. And that's a test for an odd number any time you take an odd number and then mod to is going to give you a remainder off one which says it's an odd number. All right, What about 22 mod to 0 24 mod 20 All right. That means for even numbers. If you do, mod to you will always get zero. And this is a good test for on and even. But this is so common in Ruby, there's a shorter way of doing this. How about 21 dot odd true 22 dot Odd question mark. False. So this method dot odd You can use to see if the number is odd. How about 22 dot even question mark. True 21 dot Even question mark. False. So you can use the dot even method to test if a number is even. Now one dot is underscore. Aw, question. Open parentheses. String with a capital s no method defined is ah, for one. Fixed. Um, OK, that's a bit of a problem. Let's try it out again. One dot is underscore a question mark within parentheses. String with a caps s for the class it enter. False one is not a string. And why is that? Because one is ah, one dot is underscore a question mark within cops. Integer true. One dot is underscore a question mark within parentheses. Fixed, numb. True. All right. How about hello dot is underscore a question mark within parentheses. String true are right. So what do we just learn here? We learned how to test whether something is an integer or a string. What if we do 1.0 dot is underscore a question mark Fixed. Numb falls 1.0 is not a fixed. Numb What about 1.0, dot is underscore a question mark float. True. So one dato is afloat. Perfect. Going to clear this up a little bit? What did we learn here? We learned how to test whether an input from the user is an integer a string. Now we can test that out. Right? So when we're writing a program and we're getting user input, if you want to see if it's a number, we can test it out right away are right. So why is this happening? Why can we run this? Why can we run a method on a number which is a native data type, right? Why? Why are we allowed to do this? We're allowed to do this because everything in ruby is an object. Everything in Ruby is an object, and you can run methods on objects. That's why this is allowed in Ruby. You will see a lot of programming languages where this is not allowed moving on. So we've learned how to do basic arithmetic operations. Now, what about comparisons? True. What just having here What is this? Double equals this tests for equality is five equal stew five. It's not to this that gives us in Texas air, because this, remember, is I'm trying to assign 5 to 5. And that's just not going to work. So a comparison operator five. Let's say string equals five. The number false. How about five? The string equals five. The number dot to underscore s for two string. True are right. So we're getting the hang of this. How about less than five? Less than two false. Five Less than five false, five less than equals five. True, about 10 not equals 10. False. 10 not equals three. True. So how do you say not equals? You put an exclamation mark than the equals. Okay, now let's try to side with variables. X equals 12. Why equals five X divided by why to course, because its anti terror arithmetic X do added by why dot to underscore f 2.4 perfect. So we can use variables as well. I'm going to clear this up. All right, now, how do we generate a random number? Simply Rand, do you go random number? Do it again. Do it again. Do it again. All these random numbers, right? How do we generate a random number between let's say zero and 10? Not including 10 rand. 10. There you go. There you go. It's gonna keep doing this, but it's never going to produced. 10 is the result, because it doesn't include 10. So if you want to generate any number between zero and 10 you have to give it rand 11 on. Then eventually, If we're lucky, we'll get a tan in there. There you go. All right, There it is. Okay, Control L Now what if we do this by mistake? Let's say we have five, which is a string, and we multiplied by 10 is able to give you an era. Right? Look, that's not an error. It just printed 5 10 times. So what happened here? You have a string here with just five. So if you go five plus five plus 5 10 times, it gives you 10 occurrences of five. What if you do 10 times the interest? You're five. Whoops. I said indigent. I meant string. This is an error because you can't have a string occurrence off 10. Right? You can have 10 occurrences of a string. But you can't have a string occurrence off 10 because this is undefined. That's why we have a problem here. But we can, of course, fix this 10 times five dot to I All right now it works. Now what? If you want to look at some of the methods that are available to integers, just type in three or any random integer dot methods. There you go. Lots of methods, right? We have integer. We have positive. We have negative. We have right here. Odd, even which we looked at before. Lots of methods over here that you can use. All right, now, let's look at something else that you might come across in Ruby. All right, what happens when I do? 10 dot times within quotes? High? Wrong number of arguments. I gave it arguments. All right, so this didn't work. Let's make this work 10 dot times within Carly Brace. All right. Within Curly brace, we're going to say, puts within quote high. Look at that. It printed out high 10 times control l to clear this up. All right, 15 dark times and then I'll say. But I those curly brace 15 occurrences off high. All right, so this is a convenient method Ruby gives you to create, like, a view off multiplication here, Doc Times. All right, so now that we've played around with I R B, let's exit the console. All right, let's minimize the screen. This time there wasn't any issues. That's great. Alright. So back to our playground to clear this up. All right, so we've covered a good about in this video. What I would like you to do now is enter I RB console and then play around with arithmetic operations and then strings and also perform comparisons. Get comfortable using them. All right, So play around and I are be using what you've learned so far, and I will see you in the next video. 10. More numbers!: Hello and welcome back in the last video, we were working with numbers, so we're going to continue working with numbers here, but we use IR be before, so I'm going to exit out off Irby console and we're going to write programs now. So right here. Okay, so first thing I want to do is I want to multiply couple of numbers. So let's a sign. Couple of variables. X equals 12. Why equals 10? And then I want to return the result off X multiplied by. Why so puts X times. Why save that? Let's run it. Let's see what it looks like. Ruby playground dot r b 1 20 Perfect. So it's working, but this is pretty static. Nothing's happening. There's no user interaction Now. I want to prompt the user to get these values. OK, so let's change this to first number equals 12 then second number equals 10 and then I want to say, puts first number times, second number. Okay, so that's step one. Now, Since I want to prompt the user, remember, we say, puts What do you want the first number to be and then first number, instead of assigning it to 12 we're going to say gets chomps. It'll prompt the user. And then I'm going to copy this line Command. See, Right after this Command V second number again gets whoops. I made a mistake there, gets chop okay and then puts first number, times, second number. Let's run it again. What do you want? The first number to be five. What you want The second number to be. Whoops. I mean, it was staying there. It says First number still So whatever I put in, let me fix that. Let's say it says second number 10. No implicit conversion off string to integer type error. What happened? What happened here? If you remember, this gets chomped, returns a string. So I'm trying to multiply a string with the string That's not going to work. So I'm going to need to convert this to an integer are afloat. I'm going to choose float two F and then multiplied by second number dot two f. Save that and let's fix this while we're at it. Second number. Save it. All right, let's run it again. What you want? The first number to be five would want the second number to be 10 50. Perfect and check out how it converted to afloat. It's 50.0 perfect. What is missing here? I'm not saying what it's doing. I'm simply prompting the user for a number that I'm probably user for a second number. And I'm just printing out what one multiplied by. The second is I'm not really giving much information that's not very user friendly. So let's add that in. I'm going to say puts within quotes. The first number multiplied by the second number is colon. And then this is not obviously going to work. So let's try what we did before concatenation. Save that to go back here. So run it again. First number to be five second number 10. We have an issue here, Con Cap Nation. I cannot add a number like this and then multiply another number. I can't add that to a string like this. Okay, It's not going to work. So how do we fix this? We fix this, remember? We know this thing called string interpretation so we can perform this action within this strength. Okay, So, command X to get rid of this plus over here within it, I want to say Open curly brace. Whoops. Pound open, curly brace paste that and then post Curly brace More right? So now I'm doing this within the string using string interpretation. So it's going to evaluate this over here, so let's try it out. I'm going to clear this up. First. Let's run it again. Do you want the first number to be five? Second number to be 10. First number multiplied by the second number is 50. Perfect. That is exactly what we were looking for. All right, A couple of things. You may be wondering why we do this conversion here, right? What? Instead, off when we get the input from the user, there are lots of reasons. But the first and most important is any time you get user input, let's say in this program or from the Web, you want to preserve the integrity off that input. You want to preserve it in its raw form as much as possible. Okay, user input data is very important and carries a high value with it. If you convert it to something right away, you're essentially ridding it off that roll form, and then you won't be able to get that information back. So while you want to do is we want to store it in its raw form and then couldn't afford it as we need our rights. And that is something very important to remember. User input is very valuable, very valuable. So this is a wannabe calculator we have right now that only performs multiplication. Well, that's not very useful. We wanted to perform other functions to. So let's copy this command. See? And we want to perform division over here. First number divided by the second number is right here. If I simply replace this with the slash is going to divide. So let's try that. What do you want? The first number to be 10 one. The second number to be five. The first number of multiplied by the second number is 50. The first number divided by the second number is too perfect. So now are many calculator can perform multiplication and division. Now it's time for an exercise. So I want you to write out three more of these right now we can perform multiplication and division. Well, I need you to add an addition subtraction and mod. Okay. So go ahead. Pauses. video ad in those three, and I'll wait. Okay. I hope you manage that. So this is how I would do it. I would copy this command. See, then right here. Command V two more times. The first number added to the second number is right here. Replace it with a plus sign. The first number subtracted from the second number is because of the way we awarded this. I need to replace this with second number. Second number two F minus. Over there. Over here. I'm going to say first number. Okay. And then puts the first number mod. The second number is first number two F percentage or mod. Operator. Second number. Save that. Let's run it. Forced number to be 10 2nd number five. There you go. First number multiplied by second numbers. 51st number divided by second numbers too. Added to second number of 15. Subtracted from second number, minus five. Mod off. Second number zero. All right, so it seems to be working. Perfect. All right, so it's time for your homework assignment. You see logic here, each one of these. There is a simple logic here, right? And it looks like, you know, Since we have multiplication division addition, this looks like logic that is going to be reused multiple times. So any time you come across something like this, these are candidates for methods and you have already been exposed to methods a little bit . And remember, we use this in hello world where we go define name off method. We passed in an argument. We do something with an argument and return it right. So I want you to write out five methods. One that multiplies one that divides one that adds one that's abstracts and one that finds them on. So five different methods that perform these five individual mathematical operations. All right, so I'm going to show you how to do this for the 1st 1 which is multiplication. Let's go here. We're going to start with the method definition so define and we'll call it multiply. All right. And now how are we going to pass in the first number in the second number? If you remember, you can pass in arguments, So methods except arguments right here in the hello world. Just so you remember, we passed in anything that was an argument. So here we're simply going to say any number and then comma, since we want to perform this on two numbers, right? We want to multiply one number with another. We're going to say any other number. All right, so there you go. We have a method and we have an end to signify the end of the method. Now, what do we want to do with any number and any other number? We want to multiply them. How do we do that? We simply copy this logic right here. First number doc to F multiplied by second number doc to F. But we don't have first number of second number variables. We have any number and any other number available to us within this method. So we're going to say any number doctor to f multiplied by any number. Any other number dot to wehf? Save that. So what is this going to do? This is going to take in the first number that you passing as an argument multiplied to the second number that you added as an argument and then return it. All right, So whoever calls this method is going to get back. The first number multiplied by the second number. All right, so let's see if this works. I'm going to get rid off this right here, and let's look at our program and how it works. What do you want? The first number to be? We're getting that. The second number. We're getting that. Now we have our method. So we want to call this method with first number on second number. We're simply going to say multiply. First number, second number. All right, save that. So what is going on here? Were saying, Call this method multiply first number, which we have here. Pass it in as any number second number which we have here we're getting from the user. Pass it in as the second argument. And then this method is going to take that first number multiplied to the second number, return the value, and that will be what's here. However, notice how we don't have puts here or puts here, which means nothing's going to display is just going to multiply, but do nothing with it. So we want to display the results. Therefore, puts multiply. First number, second number. Let's see what that did right here. I'll run it. What you want the first number to be 10 or they want a second number to be five. There you go. It displays 50. Then it goes on to these displays. Right? So I'm going to update this. I'm going to say whips him in the wrong place right here. I want to say the first number multiplied by the second number is and then pound open. Curly brace. I'm calling. The message will evaluate that close curly brace. Close quote. All right, See that? Let's run it, then. Five. There you go. First number multiplied by second numbers. 50 and the rest. So do your homework assignment. Go ahead and convert the remaining four into methods. All right. And make sure that the method definitions are on top before you actually call them asset. Otherwise, it will not work. What am I talking about right here? If I just take this line, Command X, and I paste it here. So if I do this, if I call this method without the method being defined above me, so it will be defined after this should not work. Let's look at it. 10 5 fails. Okay. Couldn't find the method multiply. I'm making a reference to a method that it couldn't find because it's defined below that. All right, so there you go. I'm gonna command X this and then put it back where I had called it from. All right. Okay. Perfect. Good luck in your homework assignment, and I'll see you in the next video. 11. Branching - if/else: Hello and welcome back. I hope you manage that homework assignment. This is what mine looks like. I have multiply, divide, subtract with the other two methods that I'm prompting the user to get their input. And then I perform all of the functions by calling the methods. Now, here's the problem with this. Can you see the problem with this? Regardless off what the user may want? I am performing all the functions. So I actually want to prompt the user to see what they want to do. I eat. You know, that is, if they want to multiply, divide, subtract. How do we do that? How do we implement something like that? We can say something like this right here on top. We can say puts Welcome to the calculator. And then what would you like to do then? We can say one. Multiply to divide three ad so on and so forth. I'll leave the other two out of this. So since we are asking the user, this question would like to get the user's input. So I'm simply going to call this prompt equals. Get stock chop. Okay, So I'm expecting the user to say 12 or three. And based on that, I want to perform that action. How do I do that? How do I choose one or the other? We do that by using the if condition our rights. So if conditions allow us to branch our code meaning we can do something like this, we can say if then condition right? And then if that condition matches, then will say execute this function and then we have to end to the F block. All right, so this is what a standard If looks like if a condition is met that execute whatever happens within this a block between the if and the end, and then the branch closes. So that's an if branch. Now, what is a variant of this variant of this Is else eggs acute something else. So if a condition is met, then execute the function within it else, meaning this condition is not met than execute something else and then end. Okay, so there's a branch forming here. I hope you can see that now. What if I have more than one like this? What if I have multiple conditions that I want to test for? Then you use what's called an else if, but it's not e l s e i f it's l s f so l s i f condition. Then we're going to exit cute this part off the function and then if this condition is not met or else if this condition is also not met, then else execute this. But at any point, if any of these conditions are met, let's say if this condition is met, then it's not going to perform or check for the else if or the else it will simply exit this block similarly else. If disc unde Ishan is met, he will not look at what's inside else. It'll exit this block. Okay, so that is how and if condition works. So let's apply to our program. So if what are we going to do? We're going to put in a condition right? Prompt equals. We're asking the user to input 12 or three. So one that let's start with something we're going to say puts you have chosen to multiply , right? Save that else if prompt equals two. What? You re right. We should right? You have lips puts you have chosen to divide and then else I don't need something else here . If it's not one or two, I'm just going to assume that the user wants three. So else puts you have chosen to either add or nothing at all. Okay, save that. Let's see if this works. Welcome to the calculator. Would you? What would you like to do? One multiply to divide three at let's say two. You have chosen to either add or nothing at all what happened here. That is not correct. I thought we were expecting to Is supposed to be. You have chosen to divide, right? So what happened? Ah, few things happen. Let's look at it in more detail. First of all, if prompt equals 11 here is an integer remember, Gets chomp is a string right? So prompt is a string I'm trying to compare a string to an integer So this failed. Then it went here else if prompt equals to this failed to because prompt was a string. So this comparison that we're expecting to work did not work. So it went to the else condition which waas no condition at all. It's just that if these two don't meet, then puts this. So you have chosen to either add or nothing at all, which is what happened here. All right. You have chosen to either add or nothing at all. So, what do you want? The first number to be 10. What do you want? The second number to be five. You'll still do all of it because we haven't changed that code. So let's fix this real quick. We can do two things we can either do if prompt dot to I and then compared to one or weaken do prompt equals one doc two s. So we convert this to strength right now, What is 1/3 option? The third option is I can simply put quotes around this to turn it into a string. There you go. All right, so this should now work now, the 3rd 1 I'm not really liking this, so I'm going to say else if prompt equals three you have chosen to add, and then I'll get rid of the rest, and then I will add in an else I'm going to say, puts and correct choice. All right, let's save that. Let's run it and see what happens. What would you like to do? I'm going to select two you have chosen to divide. There you go. It's working. Perfect. What you want the first number to be? Well, you know, it's still performing all the functions. So now, based on this, we can update our code instead of simply saying you have chosen to multiply. I can actually call my function. I have here multiplying a number. Any number. And where am I getting the input from the user? Right here. So I'm going to copy this. Command X. Move it over here. All right. And then let's start with Multiply. You have chosen to multiply. Now, what are my numbers? I'm going to improve this a little bit, so I'm going to say you have chosen to multiply. Pound open breath. First number with pound open brace. Second number Close curly brace. Save that. Let's run it. One 10 five. You have chosen to multiply 10 with five. All right, There you go. This is working, so multiply right here is multiply. First number, second number. Right. So I'm going to copy this entire line, command X and put it right here. The first number multiplied by the second number is multiply first number, second number. And why is this going to work? Because I have multiply. I have first number variable that I'm getting here. I have second number variable that I'm getting here. And in my multiply method, I can multiply two numbers and it returns it. So it will return at here on. Then I'm putting it out to the screen. All right. Similarly, I'm going to do this for divide Command X, and I leave it as an exercise to you to make this look better. So divide first number by second number from three edition right here. I will take this. Come on, Decks. There you go. And again, you can beautify this. I'll get released to All right, that's looking good. Let's see if it works. Save this right here. Welcome to the calculator. What would you like to do? All multiply 10 five. You have chosen to multiply 10 with five. The first number multiplied by the second number. It's 50. Perfect. It's working. Are right. So we've learned how to use the if Els branch and we've learned how to give it a condition and perform functions beast on that condition are right. So now to your homework assignment. First part. I want you to make this look better. All right, So we have a couple of functions missing, So I want you to add those functions in which arm odd and subtraction all right, and then improved this output instead of just you have Children to divide display first number of second number, you know, in all of them. And that's it. Once you've done that, copy the code and paste it. Click on the button that says Ask question or at a discussion, put in a title, Say calculator exercise, and then paste in your code, our rights. And if you cannot complete this don't worry. Ask your questions. Either myself or another student will answer your question. All right, Well, good luck. And I'll see you in the next video where we're gonna actually take this newly found knowledge off branching and use it to create a game. I'll see you there 12. How many fingers game and more branching: Hello and welcome back in this video, we're going to continue working with if statements and also other forms of branching. First, we're going to build a game, and it's called How many fingers? OK, and here's how the game works. I'm going to run it. Computer. Who was our player here is going to prompt you to guess how many fingers they're holding out and you cannot see their hands. So you have to guess between one and five. How many fingers? Their computers holding out. So I'm going to guess. Three. You guessed wrong. So that's how this game works. So let's go ahead and build it. Going to clear this up. All right, on a click here, right click new file. I'm going to say fingers game dot R B, Pull it up. Okay, so for saying is I need to prompt the user to guess right? So I'm going to say, puts How many fingers am I holding? Okay. And then the player needs to be able to respond so player response equals gets chomp. Now I need to compare this to something. Now here is the entire engine off the game is the computer needs to be able to have fingers held out at complete random, and they have to hold out between zero and five fingers. Right, So we have to use the random integer generator. So we're going to say computer fingers or any other variable name rand five. But if we do this, what's gonna happen? It's never going to have five fingers, right? So we're gonna have to say ran six. So it's between zero and five, and then we're gonna prompt a player to guess which is good. And here is where we're going to use the if condition. But before we do that, we want to display how many fingers the computer had held out after the player guesses. So we're gonna say, puts? I had since on the computer here computer fingers held out. Okay, let's see how this is working so far, I'm gonna run it. Ruby fingers game dot R b. We have a syntax error. Where is our sin doctors? Oh, I have an extra quote here. See if that run it again. How many fingers am I holding up? Five. I had one held out. Okay, so the computer is guessing. Let's try again How many fingers? Three. Four. Okay, perfect. So the computers guessing correctly? So our computer, which is our artificial intelligence here, is doing his job. We're just guessing wrong. Okay, Perfect. Now let's go ahead and add in our comparison. So how are we going to do that? We know that we have player response on. We have computer or fingers. So all we have to do is compare the two right? If player response equals computer fingers puts, you guessed right. There you go. If condition right, so flare response equals computer fingers puts you guess right now, why won't this work? I hope you guessed it because computer fingers is a random integer between zero and five and player responses a string. So we can either convert the player response to an integer here right away or weekend convert computer fingers to strength. So for this exercise, only lets couldn't afford to the player response dot to I Okay, say that. Now Let's run it. When I say five, I had one hell out. All right, You know what? Let's make this past first. So if I do, random one is going to get zero. So let's see if it's passing zero. I had zero held out. You guess. Right. Perfect. So the methodology is working. Alright, so I moved back to random six. There you go. Okay. Now take a look at this. If statement if player response equals computer fingers puts you guess right now there is a simpler or one line way in Ruby off showing this. And what is that? Let's take a look put. You guessed right. And then we can put the condition right after this in the same line. So puts you guess, right? If player response equals computer fingers and then just get rid of this. So here you're not giving it a block. You're simply saying if player responses, computer or fingers print out, you guessed right. All right. There you go. Let's test it out again. I'm gonna change this to run of one, so it gets a zero. There you go. I'm gonna say zero. You guess, right? Okay. And then I'm gonna say four. Nothing happens. All right. Perfect. So there you go. There's the fingers game. I hope you enjoyed building it, you know, play around with it and try toe build another game. That you like, based on what you know so far. Okay? And if you want to share it with us, I'd be happy to check it out if you posted to the discussions area by asking a question. Okay, moving on. Let's look at another type off branching that we can do. And that will be case. All right. What am I talking about? Let's pull of our playground right here. I have all this information here from before. I don't really need this. I'm gonna get rid of it. All right. So how does casework remember? I had multiply divide and all of that, So I'm going to first say prompt equals gets dot Trump, and then I'm gonna say, puts enter in a choice off one or two. Okay. So now I'm asking the user to enter in a choice of one or two and that I'm storing the user input as prompt. Now, how does Casework case works like this? I'm going to say result. Let's have variable equals case. That's the key word. And then what? I want to test. All right. So what is going to be used as my condition in this case, it's going to be this one right here. Prompt. All right, so case will be prompt. Now I can do something like this when one event multiply when two, then divide else don't know what you're talking about. And all right, so it basically grabs are variable. It sees. If it's one, then it says a result right here is multiply in the second case. If that's not the case, it will see Prompt is to. If so, then it's going to take divide and assigned result to divide. Okay? Otherwise, if it's not one or two, then it's going to assign. Don't know what you're talking about. Two result All right, so let's see how this works. Now we have to print out results. So puts result. Save that. Let me clear this up. Ruby playground dot r b Enduring the choice of one or 21 multiply. See how that works. There you go. That's how case works. And you may say, Oh, this is boring. I'm not really seeing how this adds any value. Well, let's take a look at another example. Okay, let's talk about exam grades, puts enter in your exam score okay. And then prompt equals gets shop. So you're asking the user to enter in their exam score? Let's convert this to I right away. OK, so we're dealing with indigenous here for simplicity. All right, so result equals case prompt. Same thing here. And if you don't like this prompt, I'm just going to say score. We're here. And if that is the case that I'm gonna have to change this to score a swell. All right, So the cases score. Now, what are my cases? I can say something like this. I can give it a range when zero dot dot 40. Okay. So what happened here? You may be asking a whoops. What is this? Zero dot dot 40. What does that mean? Well, this is how you assign, arrange or create a range in Ruby. This is saying if the number is between zero and 40 Okay, and we'll explore ranges very soon in another video. But over here, you can basically see when 0 to 40 we can say feel Then change this 41 to 60. We can say pass. And then let's add a couple more when 61 to 70 then pass with merits. When 71 200 whips. Then you're a genius. Okay, Else, I'll just say invalid score. Now let's see what that does. Let's run it. And during your exam score, let's say 75 you're a genius. Perfect. Like hearing that entering your exam score, let's say 25 I failed. All right, so you see how this works, and you see how this actually add some value In this particular case, using case actually makes sense. All right, so now we've explored a little more of branching. Which brings us to your homework exercise okay to homework exercises. Number one use case that we just learned here to rework the calculator logic. You know how in the calculator we used, if else if and else to see if the user was entering in multiply, divide, etcetera, etcetera. Use case to do that instead, off if else and one's done in the discussion area or in the forum, simply click on, ask a question or at discussion and post your code there. Number two. Over here we have K score. You saw, you know, pass, fail, etcetera, etcetera Will, based on this, assign a grade to the user, assign a grade picked the range that you want for fail D C B A. However you like, and then rework this to assign a grade based on what the users entering in as their score and then post that code as well. And those two will be your homework exercise. All right, good luck, and I'll see you in the next video. 13. Arrays in Ruby: Hello and welcome back. I hope you enjoy the homework assignments in this video. We're going to look at a raise, so I'm going to save this playground RB file and move on to my RB console. So I r b There you go, and I'm going to maximize the screen. All right, Control held to clear this up. Okay. Perfect. So what is an array? An array is an ordered list off objects or elements. Basically, it maintains an index of elements so they can be accessed at any time, very easily. So let's create an array. We're going to call it a equals. And how do you specify an array? In Ruby You put in square brackets open and close square brackets and within it 1234 Whatever you want to say, that's my friends is an array. So anything within square brackets, you put him like this. That is an array. And these elements, these elements can be anything. Let's first create this array. So now we have an array that has values 1245 67 8 and 10. These elements don't have to be integers. They can be strings. They can be array off a raise. They can be absolutely anything. I can say something like B, which is another Array. I can have it as strength mature. I can even have an indigent in there. 235 another string. Let's say, Joe, then I can have another array within an array, which has three numbers, close that array within their right and then close our array. Now, if I type in B, there is my array. So what was that in? Next thing I was talking about that arrays have an index is a way to get elements from the array. It's basically pointing to each element in the array, and the index starts at zero. So let's take this all right, right here, which is our Arabi Index zero is pointing to the first element, which is mature in Next. One is pointing to the second element, which is to in Next to is pointing to the third element, which is three. So let's access. What if we want to access? Let's say the third element, which is that index to I can simply type and be and within square brackets to There you go . It grabs that element. What if I want to grab this array? What is the index? 123456 element. Right. Sixth element off this array is an array. So be five. Remember six elements today. Nexus five. There you go. There is an array. Now, how do I test Weatherby is an array. Remember our method that we used for strings that integers be dot is underscore a question mark within parentheses. Are Ray basically testing whether b is a ray class? True be dot is underscore a question mark. Strength falls. How about be five, which is the element at index five or the sixth element off our Arabi? What if we want to test? If that isn't array dot is ah, question mark array. True. There you go. Be zero dot Is ah in danger. False. So what is B zero b zero is my sure. So is it a strength? True. So you can not only apply these methods to the array itself. You can also apply the methods to individual elements in the array. Right? Perfect. So let's now look at some other methods. We can apply to a raise. All right, let's say we have our Arabi and we want to see if Joe is included and be so. Here you go, be We know Joe is there. But what if we want to simply find out of B? Includes Joe B. Dot Include easy enough within parentheses, within quotes. Joe Wolf's There's an error. What did I miss? I missed a question mark here. Include Requires a question mark. There you go. True. Be dot Include, Let's say I want to test for John False because John is not in our array. OK, now let's look at some other methods when I clear this up. All right? What if I want to reverse the order off my Rex? I have be, Let's say be dot Reverse. Check it out. Old elements in our array has been reversed. So now 123 are array, which was the last element is first. But what if it happened? B. What's the ordering is back. What happened? It's because reverse simply displayed the array in reverse order. It did not actually reverse be. And what if we want to reverse be? What if we want to actually impact what B is pointing to that space in memory. What if we want to change that? To change that, we have to do this. Be dot Reverse. We have to use the special character is called Bang or exclamation mark hit. Enter. Now it's reversed. Now what happened? Be there you go now Array has been reversed and this is called mutating the caller and you'll see this bank used ah lot. Okay, now you may think it is consistent, but it's really not. A lot of methods require the bang to mutate the caller. A lot of methods don't, and the best thing to do is either test it out or look up the methods, documentation, whether it mutates, the caller or not. What am I talking about? Let's look at b dot pop. What is the pop method used for? Pop Method is used to pop this last idol, often array and then return the value. But then that element is basically erased from the array. Okay, the last element. So let's run pop on B and hit. Enter. You see? It returns the last element. Mature knife. I type and be notice how mature is gone. So this one didn't require the exclamation marks so it's very important to know which method actually requires it to mutate the caller and which don't. All right, now, what if I want my share back in the ray I wanted at the end? How do I do that? I could do be left arrow left arrow. That's called a shovel operator. Okay, B and then within quotes, I'll say my sure, because that's what I want to input in our array. There you go. Matures back. That Was this the only way of doing this? Nope. I could have done something else. How many elements does my array have be dot length six. I know my IRA has six elements, so the index starts from zero and goes all the way to five. What if I want the last element to be something else? What if I want to be at Index six, which does not exist right now to be John? Okay. All right, Now let's just type in B, check it out. John is there. Now what if I wanted to add John to the beginning of the race instead? Off being the last item, What would I need to do? I can simply do this be done unsure ift and then within parentheses. John. Now check it out. Now, have John in the beginning as well. But see, now I have to Johnson here and I don't want duplicates. What can I do? Be dot Unique u N I Q. There you go. John has removed from the end because I only wanted one instance of John. However, if I type in b notice how John is back? Because I did not use exclamation mark when I used a unique method. So beat our unique back. No, I haven't. B. Check it out now. What if I want to mess up the ordering off this array? What if I simply want to shuffle all the elements? I can do something like this be dot truffle and check it out. It travels it. But if it happened, be the ordering is back again. Be dot shuffle bang. Now the ordering is messed up. Not what, Evan B. C Now it's a different ordering. Okay, so you can use the shuffle method to do this, and I'm gonna clear off the screen. All right, now, let's say we want to create a range in Ruby. Basically, have an array with a range of numbers, and we don't want to individually type it out. How do we do that? We do that by specifying a range and converting it to an array. So let's look at it. Zero don 00.99. Let's say we want a range off 99 numbers, okay? And then we want to run the to underscore a method which is to array to this range. What happens? Hit. Enter. You have an array off 99 numbers. Actually, you have an array of 100 numbers because it includes zero as well. So now we have this range that we created. All right, so what if we want to shuffle the ordering off these numbers? How do we do that? In Ruby, you can do what's called chaining off methods. So let's do this. 000 dot dot 99. So we have the range dot to underscore A to convert to an array and then dot shuffle. So now, not only are we running this doctor way method, we're also running the shuffle method on that element. So what happens here? Now we have a random ordering off our 100 numbers. So this is an array with random ordering off the numbers in it. All right, But what if we want to create an array with this that we can actually reference will use this and then the beginning? I'm just going to say a equals. So our array is going to be this. Hey, there you go. And I want to shuffle it again. So a dog shuffle bang. So now it's reshuffled travel twice, and they're obviously lots of ways that this can come in handy. But think of it from a game perspective. Let's say you're creating a game and you have a deck of cards. You have a deck of 52 cards. Now you have a way that you can have a random shuffle on your deck of 52 cars, right? So each one you can run this shuffle method and it will shuffle your deck. So let's take a look. Could have something like this. Card order equals one. Since you don't want to include zero as your cards to 50 to 52 cards docked to A you're converting into an array dot shuffle Bang. There you go. You have a random ordering off 52 cards. Now, if you want to shuffle again, you couldn't do card order got truffle bank. I have another ordering all 52 cards. And now you can clearly see that as long as you have the value of each card defined, let's say one is the ace of spades to is the king of spades, etcetera, etcetera. You can have a deck off shuffle cards using something as simple as this. All right, Now, let's look at some other method. So we have this array, right? We have our card order card order. Which is this Control Elder? Clear of the screen. Okay, now, what is my array card order? There it is. What if I want a 53rd card? What if I want a random joker card and added it right on the end? How do I do that? I could do something like this. Card order dot Push 53 lips. I have ah issue here. Oh, I wrote card odor. That's not good. Card order, Doc. Push 53. Check it out. It adds it at the end. So pushes another method that you can use. All right. What if I just I'm going crazy. And I wanna add in like the number 100 to this. I can do card, order, Trouble 100. Then I make a mistake and I type in 100 again. Now have to hundreds. So remember, if I want to get rid of duplicates, all I have to do is card order dot Unique bag. There you go. The duplicate 100 is gone. So one other things. When you're working with a data structure like this, like a array and you will come across other ones as well that you're gonna need to be able to do is iterated through them. You know, like it ary through all the elements and perform functions as you come across an element. Or as you come across any of element. Let's say I want to print out the value off each element to the screen, iterated through the entire ray. And as I come across each element, I want to print out the value. How do I do that? So, in that case, what you would need our it aerators. All right. And Ruby has a preferred it aerator. What is an it aerator Honore and it aerator iterated through this array so goes to each element and performs a certain functions on it. All right. Could be mutation. Could be a comparison. Could be a comparison, and a mutation could be anything. Rubies preferred method for Iterating through Honore or a collection of elements is the dot each method. So card underscore order dot each and I'm going to say, Let's say I want to print out the value of each element. How do I do that? You do that by giving it a block. Okay, open curly brace. Close Carly brace. So card order, which is the array dot Each meaning each element in the array and then do something. So how do you do something to each element? First we have to grab each element. So as I go to each element, I'm going to use these to these air called pipes. Okay, within it, let's add put an eye. So I assigned the value I. So as we use this dot each method to enter it through our array. First time eyes going to point to 17 then eyes going 2.26 Then I going 2.25 So I basically will be pointing to the element as it generates through the entire erect. All right, so card order dot each I What do I want to do with I? Let's say I want to just put I well, this do each time you will just print out the value off that element and then move on to the next line. Let's see if that works. There you go. It printed out each of the elements and then moved on to the next line and then put it out . Now, why do I have this year? Why do I have this entire array printed out again? That's because Ruby, remember always by default, returned something every expression, every method in Ruby in return, something. So this just returned the entire array that we worked on. So it first performs the function. Then it returns the entire right. Okay, control l to clear it up. And that's a lot we've covered in this video so far. So I'm going to stop here, and then we're going to continue in the next video. See you there 14. Arrays continued: So let's look at our Reagan card order. Okay? So before this, we were accessing elements like this. Right? Card order. Let's say seven. So it will grab the eighth element. We have eight. Element is what 30? There you go. 30 card order seven. But now we have this iterated that we can use. We can use the dot each method and then grab elements as we need. All right, so let's look at this in a little more detail. So this so this open curly brace and close curly brace. This is actually called a block, and it's very important to remember. All right, So you're giving these instructions within the block. I can change this instruction. I can say print I, and then it prints all of them, you know, puts actually puts it in a different line. But print, if you remember, doesn't add a new line. So there you go. Now, what if I want to say print I only the number is odd. I can do this print I If I dot on question mark Now, we only have the odd numbers. 17 5 41 21 All right, so what if I You know, it's not clear here because we are not doing it in a new line. What if I do puts, Then it will be clear. There you go. Only the odd numbers have been printed here. What if I want to grab only the even numbers? I can run this even There you go. Only the even numbers are printed here. Now, you can run different kinds of code within the bloc. And you could basically specify this, which gonna be very powerful, right? So control l to clear this up. Now, one thing to remember is traditionally for doing something like this where we're using the dawn. Each method we used to use four loops. Okay, so what is? All right? Card order. So card order. There we go. Now what if I wanted to use a for loop to run through this? It would look something like this four. Okay, I or anything, whatever we were using in pipes like this. So each element was I we were saying so for I no need for pipes in card order, which is all right. And this is how in ir be You basically write a method or you basically go through a block, okay? For I in card order, I'm going to say, puts I And then I'm going to say end now. I'm within the bloc. The reason this is not executing it is because I'm still within the bloc, so end. And as soon as I end the block, there you go. It runs through it and it prints out each one because that's what I'm saying for it to do so. Here's the block. What if I want to do something else so we can do something else? Let's say we want to print out something else for I in card order. I want to say put high and then the value off. So pound open, curly. Brace I then. And there you go. It prints out high and the number, which is what we specified here. Right? So let's try that again. Control L four. I in card order puts high. That's it. I'm not bringing out the number. It just prints out high as illiterates through each element. All right, so that's something interesting. So that really doesn't do anything. But that's just for demo purposes. All right, there we go. So let's look at an array of strings now. OK, so we're going to say names equals within quotes. I'm going to say mature than John on Joe. Andi. Chris. OK, so having array off four names, right? What if I want to capitalize each name? What? See, all of them are lower case and Lorca's John. What if I wanted to? Em to be caps J to be caps and so on. How do I do that? What I'm gonna do is I'm gonna use a block. Remember how we used a block before? I'm gonna use the block, but in a different way. See, names are each. This specifies a block, right? So I can grab each element. I can say name instead of I can just send name. And then I could say, puts name dot Capitalized. Check it out. It prints out the names. Capitalize. So what's a different way of this? I can actually use a block to do this, So let's look at a block. Names, not each do. That's how you specify the start of the block. Instead of this, you're basically using do and I'll say name. And what do I want? Each block to do. I wanted to say puts Hello, pound open, Curly Brace name because I want the value of name close quote and then end. Hello, my Sure. Hello, John. Hello, Joe. Hello, Chris. But I want to capitalize. Right? So let's start again. Name's not each do name. Then I want to go puts. Hello. Name with that? I can simply say capitalize. There you go. Perfect. Now another method I want to show you Here is the select method. Okay, so let's create an array A and we're going to say, you know, 100 So zero 9900 integers 99000.2 way So I have 100 integers. Remember, we use the daughter used to test for if only print out element. If it was odd, we can do this using select as well. How does that work? Let's look at select. Let's say a dot Select and then you give it a block just like you do with the dot each right and within it each element. So I'm just going to say I hear within pipes. And then what does it do here? This takes in Ah, Boolean expression and test for that expression. Okay, So if I want to print out only the odd ones, I can simply say I don't on. If I do that, then he will only select items based on if they're on. Check it out. It does that. What if I want to do something else? Like even? There you go. So it's something similar. But you can use the select method to do this as well. All right, Now let's take our array. We have our names are a right. So names. We have this and I'll control l to clear this up. So there we go. I have names. What if I want to add each of these names? What if I want to add them and turn them into one straight? How do I do that? I can do names. Da. Join. There you go. I have a string now All of them are joined. But there is no space in there. How do I add a space? I could have done something like this. Name star join and then within parentheses. I'm going to give it an open space. There you go. Now it's displaying the name separated by space. What if I want to combine their names dot Join comma space. There you go. More sure. Jonjo Chris. So we have our four names separated by comma in a space. Now the original one is still that What if I wanna new names? I want to say new names equals names dot join and then, you know, with the Kama within quote space. There you go. So I'm basically gonna join it and call it new names. So new names is this string that I have? What if I want to convert new names back to an array off elements separated by, Let's say, comma space. How do I do that? First, if I do the new names dot split Look at that. It just splits out each element based on the space. What if I want to split it out based on comma? Can do new names, lips I can do new names, stopped split on, Then within it, I'm going to say comma space. There you go. And now it's splitting it out based on common space. So it looked at new names which has this comma space in there and for each common space, it turned out into an element. So mature Jonjo Chris now have an array off strings. Perfect. So that's a lot of material we covered in this video working with a raise and all these methods. So I'm going to strongly recommend that you go over this video again, type out content, create your own arrays, use methods on them in practice, do things to the Rays, right? Do things and see if what you are attempting to do is what actually happened. Get familiar with using them. They're gonna be very, very important. All right, So with that we conclude this video in the next video, we're going to look at another data structure which is called a hash. See there. 15. Hash: Hello. And welcome back in this video, we're going to talk about hashes. What is a hash? Just like an array. Ah, Hash is a data structure that stores values. But unlike an array, they have what are called a key and a value associated with each element. So let's take a look. I'm gonna copy paste a hash over here. Do you go my details? I've named my hash. My details. And the way you specify a hash is with this curly braces, right open and close Carly braces that specifies that this data structure is a hash. And within it, we have an element, which is the key, the 1st 1 and then this is called a hash rocket equals and then right arrow. Okay. And this is syntax used in hashes. So the key points to this value. Then you have a common to separate out. Elements are second element in our hash. The key is this five color, and then the value is red. Okay, so let's create a new hash based on this. So we're going to say new hash equals first things first. There you go. Open and close. Carly brace. Meaning this is a hash. Now, here, I'm going to use the new syntax that's used for hashes. Okay, so you will see this quite often. You will see a key like this, a colon after that. And then space and then the value. So what's going on here? This looks very different, right? Because it doesn't have the hash rocket. This specifies that a is a symbol. A symbol is just like string an integer. It's a different data type and a symbol. So we're saying is a symbol and a has value one. Okay, so I'm gonna give it another one. So a different one B that I'm gonna say B has value to and then see as value three. Okay. And what happens if I do this? Check it out. It says a one b two c three. And this is the same as new hash. Old style. So that's the same syntax as this. Essentially, colon A. Which is how symbols are represented in ruby. So Poland a hash rocket, one colon be hash rocket too. Cool and see hash rocket three. There you go. Now we have a hash. This is the exact same hash as new hash because look at the result. Even though you specified it like this, it still gave you colon a hash rocket, one colon be hash rocket to Colin C hashtag etc. Which the same is this. This is the old form of displaying symbol s key and value relationship. This is so common. Basically, it got simplified and you can simply use the symbol colon and then the value. And that would work as well. Okay, so now how can we access a specific value associated with a key in our hash? Let's say I want to know what the value off B is. How do I do that? So I can say new hash open square bracket. And then I have to specify the key. So cool and be close. Square bracket. There you go. It gives me the value off, too. So this is saying be has value to what? If I want to add an item to this hash, Let's I want to add D and make it nine. How do I do that? I can say new hash and then within square brackets, Colin D will square brackets equals nine. All right, then, if I'd happen new hash Notice. How d with a value off nine got added to my new hash. Right? What if I want to add another one? New hash? I want to say E and I want to give it a value off 11 now. New hash. Now, what if I want to add in a string as a key instead? Off using symbols? I can do that as well. New hash within square brackets. I'm just going to say name. We'll square brackets equals within quotes. Assure now. I thought I have been new hash. There you go. Now, this one, the key is named, but it's a string, not assemble, and value is mature to access. This I cannot simply say new hash colon name that will not work because this is a symbol name. This is a string name. What if I added new hash and then symbol? Name equals? Yeah, sure. Now, if I type in new hash, check it out. I have a symbol name, which is the key value mature. So if I type in new hash and then name now, now it's going to return mushroom or perfect now, just like an array. Your element as you've seen can be any type. They don't necessarily have to be strings or integers. You can have symbols or other things as well. You can have hash off. Ah, hash. You can have an array off hashes. You can have Ah, hash full of a raise. You could do whatever you want in here. Okay? It's completely up to you. Now. What if I wanted to delete to this one? This one that says name Mature. Right. What if I want to delete that? You can simply type in new hash dot Delete. And if I give it the key, it will delete the value as well. So name it returns. What? It deleted. If I type in new hash, see the string name with value. Mature is gone. Here you go. Let me clear the supple. Oh, okay. Now, what if I want to reiterate through this hash? Remember it, aerators. We can use the dot each method here as well. So let's do that. We have new hash, not each. Okay, give it our block. And within the bloc, instead of just having I or number or key, we are going to be working with key and value pairs for hashes. Right? So within the pipes, not only do I have to give the key, but I also have to give the value. Okay? And again, you can make this K and V or whatever you want. But you. Since you're working with key value pairs, you have to make it look like this. All right, so new hash dot each. Gavey, what do I want to do? Let's say I just want to print out the key, then puts K. It just prints out the keys in my hash, right? What if I want to simply print out the values it prints out the value associated with all the keys? What if I want to print out both key and value So instead of puts V, I'm going to say, Put the key is pound open, curly brace K. And the value is power open. Curly brace v. Right, because I'm saying V's the value ke is the ki at each generation, right? So when a close quote, there you go. The key is a and the value is one. The key is be value is to its iterating through and listing out the key and value for each item in our array. Now, what if we want to do some kind of function or, like, check for a comparison or something like this? And based on that, we want to perform a function on the elements in the hash or on the items in the hash. How do we do that? Okay, we are on the screen a little bit. Now, Let me create a new hash over here. So new hash and I'm going to say it a one B two C three. Okay, There you go. So I have a hash with six key value pairs. Now, what if I want to iterated through this hash and delete the key and value pair? If the value is odd, how do I do that? I could do something like this. New hash dot each and then I can say, you know, key value pair. So within pipes, K v New hash dart. Delete. Okay. If v meaning the value dot Odd. Right. Let's try that close. Carly brace. Let's try that. There you go. So now our new hash is just 24 and six and got rid of 13 and five. Okay, What if we take this new hash again? OK, so let's grab our new hash again. Okay? This is our new hash now, And what we want to do is, let's say instead, off if odd, I want to delete if the number is greater than three. How would you do that? Simple enough right here, if the greater than three. So there we go. You have 123 in Got rid of 456 Okay, so that's a brief intro into hashes. Now, if I type in new hash dot methods, check out all of the methods that are available for our hash. Right? So let's say this one length new hash dot length three are right. What about size? New hash docked, Size three. So, like this. Go ahead and try out different methods on hashes. Play around with hashes. Review the video. If you need tryout, you know, creating a new hash with multiple key value pairs and, you know, practice because practicing is going to create muscle memory and you need muscle memory in your fingers in order to efficiently right out cold when you need to it, instead of having to look things up all the time. Okay, so go ahead practice and I'll see you in the next video 16. Ruby style guide: Hello and welcome back in this video, we're going to take a little break from our curriculum and talk about styling and ruby, which is very important. If you have a public repository and you're showing potential employers your code or anything like that, the style you use, it's going to tell this potential employer or a partner or whoever. If you really know Ruby right and stalling. And Ruby is very important. So let's look this up. I'm going to first get rid of all this code from Playground. First things first Snake case. What is snake case? Snake cases. Something like this. Basically, first underscore. Name your underscore world. That isn a case where you have the word separated out by underscore and no capitalization. All right, let's add a couple more examples of snake case. You have current underscore, user. Let's say logged in. These are all state case. So what do we use may kiss for? We used a case for variable names, right? In Java script, you would have things like snake case as the variable name. We don't want that in Ruby in Ruby, we wanted, as in a case, right, another use off snake case is method names. So let's say we have this one current user. Right? So if you're defining a current user method, you would have something like this. That would be your definition. The method would be called current user in Java script or in some other languages. You would have something like this, right? Current user. We don't want that in Ruby Ruby styling convention. Is this all right? Another thing is right here on the bottom. Right. You see, spaces is two very important to have space is at two and then convert two spaces. All right. These two are ruby conventions. They're very important to have. So what does that mean? That means when I tab, it goes to space is instead of four. Okay, now, how do we tap? Let's say, define current, user, and we have end any cold we have within it. We want to tab in one. Okay, Now within defined current user. Let's say some code, and then we have an if condition, so they have conditions. If condition will start here, if condition right, then whatever we have within the F condition and I'm going to say end here to signify the end of the block. Whatever code we have within this condition needs to be tabbed in also. So, if condition, I'm going to say some code. What about if I want to have an else else should be right here, aligned with if okay, else should not also be topped in. Because if some condition else, some code and then end. All right, So this is how an if condition should look like within a method. Let's say we're going to have X doc each within this. Right. Do something. So whatever we have within this do an end block. We want to tab in and write our code here. Okay, So this is how indentation works in Ruby, and this is how variable names and method names are defined. Snake case now, another thing, which is not important right now, but will come in handy later on. Is camel case for plaice names. So when we get to this and again, if you don't know what this is right now, don't worry. If we have a class off type, lets a user, it's going to be camel case. And what is Camel case? Camel case is basically The first letter is capitalized off. The name so user. All right, let's say we have another class on the class. Name is users controller. Then how are we going to have it written? Camel case means users controller. Each word within the name when it begins needs to be capitalized. No underscore here. Okay, So you was capitalized, See? Is capitalized. So that's how you have class names. All right. And we're going to explore this later on. Don't worry. If you don't understand what that is yet, that is completely fine. OK, now the homework assignment full of a new tab and type in ruby style guide in Google, and it will bring you to this link. Get ha blink. It's be bats off. Ruby style guide. Scroll down. I want you to read through this, okay? It has a very nice table of contents and everything. I want you to read through syntax, naming comments, Comandante Shins, I don't want you to read the whole thing. Read only up to this. Get to classes and modules and you don't need to read anything below that, but read everything above it. Okay? And once you do, I want you to write out to things as homework exercise and post it to the discussions by clicking on Ask questions in the Q and A section and simply put in the title as style guide questions and in the body off the question paced in your code. And what do I want you to write? I want you to write one message with the correct name. Okay, name format and then within it. I want you to write if else branch. Okay. And then make sure the indentation is correct. Make sure the naming is correct. That's it. One method with correct indentation, correct method name correct. Variable name usage and if else branch within it. Okay. All right. Good luck in the homework assignment. And have fun reading this and I'll see you in the next video. 17. Homework - zip code: Well, welcome back. This is a video where we will discuss your homework exercise so the homework is going to be a zip code Look up Dictionary. Okay, zip code. And a lot of you may have area code or whatever you call it, but it's basically it's just like a telephone area code. OK, these towns, for mailing reasons, have ZIP codes assigned to them that you add after the city and state. So it's something like that without going into much detail. Let's look at what the program does, Okay, so I'll run it. This is what it will do. So when you run the program, its first going to prompt the user, do you want to look up a zip code? You can say no and exit the program. Okay. If you say yes with a lie, it's going to give you a list of cities in the dictionary, and it's going to ask you with city, Do you want to look up the zip code for? So if I type in New York, for example, it enter the zip code for New York is 10012 All right, then it goes. Do you want to look up a zip code, and then it keeps going on. So here you, some of you may be thinking, Are there multiple zip codes for New York? Well, this is a very simple exercise to use methods and a hash as an input to basically figure out zip codes. And, of course, you could make this as advanced as you can. So let's look up another zip code and I'll say Edison. And then it says, Okay, the zip code for Edison is always a 37 How about look up another zip code and I'll say, Atlanta, which does not exist among the ZIP codes among the city, is in our dictionaries. If I do that, it says the area you entered is not in the zip code dictionary. Do you want to look up another zip code? I'll say hello and it exits. All right. So unless I say yes, the program is not going to continue. It gives you a list, and it basically runs through, prompts the user to entering the city and then proceeds to give the zip code associated with that city if it exists. All right. So what do you need. You need something like a dictionary like this. Zip codes equals and then have it as a hash. I'll show you how I did it for a New Brunswick, for example. I'll say right here, New Bruns Wick within quotes, New Brunswick equals and, um, then I said, Close quote their open other quote. 08837 So this is how I had it. And then I would have New York. OK, so that's how I defined the zip code or the dictionary. Then you're going to need a couple more methods. You're going to need method to display city names in a dictionary. So here you have a dictionary or a hash and notice how the key is always the city name right? So basically have given a hash. A method simply returns all the city names, and that's what we used here, where displays all the cities that are listed. So you want a method to display that. Then you want a method to display the value off key value pair so method to display the value off a key value pair right? So let's say I give it 1/2 like ZIP codes and I give it the city. Let's say New Brunswick or New York, this method has to be able to return the value associated with that key. OK, And then the third thing you're going to need is a loop. I'm going to call this out temporarily. And so this loop is going to be for this mechanism that keeps going, you know, till you press a N or something else. How do you do that? You can do that in a lot of ways you can use while loop or something else. But here, what we're gonna do is this loop do. And there it is. Now within it. We're going to say something like this. What do you want to play the game? This is what I'm using here. But obviously for your homework, you can say zip code. Look up program. Do you want to look up another zip code or something? And then you prompt a user So what's gets trump if prompt, not equals? Why Break bend. Okay, So if prompt, not equals, why then break meaning in or anything else? If the user enters is going to break and so you'll enter in all your code below this right here. Well below this end, you can enter in put Hello. We're playing again. So let's see what that does to run this program will be playground Darby, Do you want to play the game? Yes. Why? Hello. We're playing again. Okay. Why? Okay, we keep playing and it short circuits. Okay, let's try again. Do you want to play the game? Now? Here's one thing you can put in caps. Why it short circuits to Because I'm comparing to lower case. Why? So what you want to do here is prom. To get start, Trump would change it to down case right away. That way, regardless of whether the user enters caps, why or lower case? Why, it will compare it to lower case. Why? Basically converted to down gets right away. So let's try that. Caps. Why? Still playing. Okay, Perfect. All right. Okay, So that's your homework assignment. Obviously, the solution will be provided after this. Good luck. And I'll see you in a nice video 18. Zip code homework project solution: Hello. And welcome back in this video, we're going to work through the solution for the ZIP codes. Homework. I hope you are able to complete it. If not, no problem. We'll look at how toe build it step by step. So first I'm going to get rid of all this code I have in my playground. And we're going to start with a dictionary with zip codes. So I'm simply going to copy paste this one. There you go. So there is my dictionary. I have zip codes. I have New Brunswick. I have New York Edison and Weehawken four are more than enough to get started and get this to work. So if you remember, what do we need to do? First, we need to run a loop and see if our user wants to look up area codes. Right. So let's do that. So, like, we discussed in the last video run to run a loop right here, So loop do and there's the end. Okay. Perfect puts. Do you want to look up a zip code question mark within parentheses were going to say caps. Why? And then caps in so that the user unders? Yes, or no. Okay. And then I'm going to say answer equals gets Trump. But remember, what if the user enters capsule ire? Lower case Why? We want both toe work. So I'm going to convert the answer regardless of what it is to down case. All right? And then, if answer not equals than quotes, why? Since it's down case, we're going to break on, then end. All right, so there's our loop. So let's run this and ensure that this is working. Ruby playground dot R b. Do you want to look up a zip code? Why? Okay, smaller case. Why? Perfect and okay. Perfect. So that functionality is working. So what do we want to do? At first? We want to display the cities or areas that are available to display the zip codes for right. So right after this, we want to say something like choose a city or choose a city name once the user says, Why over here. Okay, so let's do that. We're going to say, puts Which city do you? I want to look up Zip codes. Four. Question mark. And then what do we want to do? We want to list out all of the cities that we have so we can do something like this we can do puts zip codes. Let's see if that works. All right? Yes. Okay. Once I said yes, it said with city, do you want to look up zip codes for And it lists out all the cities and the zip codes in a hash form. Okay, We don't want that. That looks kind of bad for one. And secondly, it also list out all the zip code, so it beats the whole purpose of our program. What we want to do is we want to write a method now to get a listing off on Lee the city names and to list it in a friendly way. Okay, so let's do that. I'm gonna get rid of this. I want to write a method over here. Now, this method is going to take in Ah, hash. It could be any hash. It could be our zip codes after or any other hash takes in the entire hashes an argument and lists out only the keys. So it generates through the hash and lists out the keys. So define, get city names, and then it's going to take in any hash or simply going to say hash over here and then end to the method. All right, so there we go. Here's our method. Now we want to iterated through this hash, right? Hashes getting past in. So imagine that this hashes getting past into this message when it's getting cold. We want to do hash. Don't each because we want to run through each element off the hash and then we have to give it our block and within it. Remember, hashes are key value pairs. So we're gonna say key and then value, and we want to simply display the key. So puts key. Okay, so there you go. There's the adoration, and it's going to print out the key, so let's see if this works. But right now we need to add something to this because we actually need to call this method after this foot statement because right now we have this method, but we're not calling it and we're not passing in any hash, so I'm simply going to say, get city names and then I'm going to pass in the hash that I want this method to run on And that hash in this case happens to be zip codes. So I'm going to pass in zip codes. All right, save that. Let me clear this up. Let's run this, OK? Do you want to look up a zip code? Why? Okay, there you go. With city. Do you want to look up? ZIP codes four. And it lists out the city's perfect. Do you want to look up a zip code? Also Hello to end it. All right there. You Okay? So now we have a method to list out the city names. What's the next step? The next step is I want to get selection from the user after displaying these city names and to verify that that selection actually matches our cities listed. So let's do that. So here. So let's problem the user first. Now, here instead of puts, I'm gonna use print because I want the entry to be in the same line as the statement. So enter your selection. All right? And then we need input from the user. So I'll say prompt in this case equals gets chomp. Now, what is the next step right now? If I run, this program is going to get this election from the user, but then it's not going to do anything with it. So we need to do something with it. We are going to first test whether our hash over here includes the city that the user is in putting. Okay, so how do we do that? Remember, for hash offer, right? We can use the include method so we can say zip codes dot include question mark. And then what if they enter in New York? Let's hit the enter in New York, but they enter it in with caps, then it won't match because this is lower case, right? So I want to convert. Prompted a two down case, prompt dot down case. All right. And if this passes meaning it does include what the user is in putting that I want to say something friendly like okay, you've entered the right thing or the ZIP code is so I'll say for now, the zip code for rond is, and I'll leave it empty because we don't have a method to get the zip code yet, But at least it's going to say the zip code for prompt is okay and prompt is the city name now? What if it does not include it? What if the user enters a city name that is not in our zip code dictionary? Then I want to say the area you entered is not in the zip code Dictionary. Okay? And then since it's a if else block, I need to put an end to end the fells block. So let's see how this works. I'll run the program. Do you want to look up a zip code? Yes. There you go. Enter your selection. I'll first do New York. The zip code for a New York is okay. There you go. So this part is working right now? What if I put in caps? New York? So, do you want to look up a zip code? Yes. Going to say New York. Okay, so this is also working. Perfect. What if I put in something incorrect? I'll put in London? The area you injured is not in the zip code dictionary. Perfect. So that's working as well. Great. So I'm gonna exit out of here. So what I want to do now is I want to list out to zip code off the area that the user entered. So how do we do that? We need a method to do this. We need a method that's going to get the value associated with the key value pair for the hash that's passed in. So let's look at it. We're gonna say, Get a zip code. And what is this method going to take him? This method is going to take in a hash, so I'll say hash. And it's also going to take in a key value, right, because we need to know the value associated with the key. So we need to pass in the key and remember from the user we're getting the key here because the user is entering in New York or New Brunswick or whatever it is. So we will have the key. So we're gonna need to pass in the hash and the key, and I'll end the method. So how are we going to display the value associated with the key from this ash? This is very simple, right? We have done this a few times already. We have our hash, which we're getting here, and then we want to simply return the value, which is associated with this key. So this is the syntax for that which we have already seen. We've seen something like this, right? We saw test hash, and then within it, we saw we did something like this. And then this was returning the value for key A right which was one or three or whatever. We did something like that. So we're simply going to do this. And if you're wondering, why am I not putting a puts here? Because I need to get the value returned here. I don't need to print it out to the screen. I need the value returned here because I'm going to use it within another puts right here. So in Ruby, if you remember by default, this is the same as return. Hash key was the same as this, but that return is implied. The last executed statement in a ruby method. It's automatically assumed by Ruby that you're saying return this so I don't need to do that. I can simply say hash key. So the return is implied and right here, the zip code for prompt is now. I need to call this method right. I'm already inputs, so I don't need to worry about. Printing this out puts will take care of that. But I need to call the method. So right here and within it, I'm going to call the method, so get zip code. And then what do I pass in? I need to pass in. Ah, hash. And the key. Right. So what would the hash be? The hash is simple. We already have our ZIP codes hash right here, which were wondering where that came from. There it is. Zip codes. Okay. And what is going to be the key? The key is prompt, but it's not Just propped its prompt dot down case to make sure capitalization is taken care of. All right, that we don't have capital letters in here, So prompt dot Down case. All right, so I'm passing in the key and I'm passing in the hash. Let's see how that works. Do you want to look up a zip code? Yes. I'm gonna say New York. The zip code for New York is 10012 Perfect. It's working. Let's try again. Won't you say New Brunswick? The zip code for New Brunswick is awake. Nine. No one. Okay. That seems to be working as well. Perfect. Now let's enter an invalid. I'm going to say Hain again. Area you entered is not in the zip code Dictionary. Do you want to look up another zip code? No. Perfect. So there you go. There is our zip code dictionary. I would recommend, you know, go through this. If it's not clear at first, type it out a couple of times. Pay attention, especially to when you're calling the methods. How are you calling the methods? What you're getting in return. What are you passing in As arguments play around with it, Okay. And if you want to come up with a variation of this feel free to even has these methods, it works with the hash and, you know, performs a certain function with a loop in it. That's perfectly acceptable. Please pace that code to you know, the discussions. Click on, ask a question and then pasted with a title for the homework. Okay, the hash homework. And I hope you enjoy this exercise. And I look forward to seeing your code 19. Practice what you have learnt!: Hello and welcome back in this video, we're going to take a little break from our regular curriculum, and I'll ask you to practice what you have learned so far. So easy way of doing that club Google and type end. Try ruby dot org's and that will bring you to this page. This is an interactive ruby training guide, and it walks you through some basics off Ruby. And while this may not have too much in depth information, it does provide a good interface to practice what you've learned and also expose you to some new things as well. Okay, so go ahead. Go through this free interface and practice what you've learned and I'll see you in the next video. 20. Build a game - procedural programming: Hello And welcome back in this video, we're going to build a procedural programming game. Okay, this will be kind of the conclusion to our procedural programming if you're wondering what procedural programming is. That's what we've been doing so far. The other aspect to this as object or in and programming which will start after this. So what do we want to do? We want to do something that kind of takes everything we've learned on, put it to use. Right. So what we're gonna do is we're gonna build a card game. We don't know what this game is exactly going to be yet. Alright, we're just gonna build that with this. Will involve shuffling a deck of cards and we know how to do that already, but we're gonna learn how to use it. We're going to display it now. Here's the tricky part of our card games. Ah, lot of the cards have the same values, so we're gonna have to learn how to assign values to cards. We're also gonna have to give them different names. So let's say what do I mean by that? Let's say we have a card deck and we have speeds. So, King of spades, Queen of spades, jack of spades and 10 of spades all have value. 10. Okay, So usually in casino games like blackjack, there's no difference between a queen of spades and the jack of spades. They're both value 10. Right? So we're gonna have to figure out how to work to that. We're gonna have to display them. We're gonna have to explore here. I want to show you how to explore, Like, you know, when we think up. Ah, programming scenario and what we want to do. I don't want to sugarcoat districts exercise at all. I'm not going to give you optimal code. This is going to be something that we explore and build as we go along, because we don't even know what game we're building, All right. And I'm gonna leave that to you at the end, and then I'm also going to ask you to add features to it and make it your own, but they get that's later. We're gonna do the set up for us, right? First things for us. We need to get a deck of cards, shuffle it and display the values. All right? And if you remember we used arrange to do this before, so let's do that. So let's get a deck of cards. We're just gonna call that deck. All right, equals. And then it's gonna be 52 cards. So 1 to 52 right, So that there's my range. I want to turn this to an array to a dot and then I want to shuffle it, so I get a random ordering of this. All right, And then I want to display it. Puts deck. Let's see what that did. All right, let's run this. This is the X three, so ruby e x three dot R b. There you go. There's my deck of 52 cards. It showing all these random numbers random shuffle. Okay, now, what happens if I do print to deck? Let's run that much better. So we have the 52 cards right here in a random order, and I want to display this in the next line. So print deck, Then I'm just gonna put on m d string. Let's do this again. All right? Now, if I keep running this every time is a different order off the 52 cards, okay? So now we know how we're going to build our deck of cards. Each one can have this number or number of value associated with it. But these values are not going to correspond to the value of the cards because the value of the cards can go from 1 to 10 and then the ace can be in 11 as well. So we're gonna have to figure that out. We're gonna have to figure out how to do that. How do we do that? If you look at this, we haven't array here. And then if we're saying each of this, let's say 18 is a card, so 18 will have a card associated with it, and then it's also going to have a numeric value, which will be 18 that it's also going to have a value for the card itself, which could be something else. Then how about we just take the form we have here, which is an array? And then each element off the array is ah, hash? How about that? Let's write out that when we get to practice, both right, so let's build this deck of cards. This is what is going to look like we have card details, I'll call it okay, and then this is going to be in. All right. Right. So open, close, square bracket. And then what do we want in this? We want each one to be Ah, Hash. So let's take the 1st 1 Where do we have number one? Right here? If I can find it, There it is. There's number one. So we're gonna take one, and we're going to call it a self speeds. All right, so there's my hash now within the hash. I'm gonna call at East off speeds. That's what I want to call number one. No, A self spades is the key. What do I want is value for it again. I'm experimenting here, So the first thing I can think of is a numeric value, which will be one. So I'm going to say, numeric numeric underscore value, and I'll say it's one. But look, now I have another half structure here within the hash. So let's do that. We're going to say yourself speeds, which is the key is going to have a value that's going to be a hash as well. And so numeric values going to be one, and then the actual value now is so Speed's gonna be one or 11 in blackjack. But for simplicity's sake, I'm just going to start off with 11 okay? And then close this out. All right, so now I have, ah, hash off a hash within an array, which is my card details. All right, but it's taking shape. So we have a self spades defined right? So now my second element, what do I want? I want, let's say, king of spades, similarly king off spades, and I want to say numeric value I'm going to give it to, and this will associate again with this array right here. There's too, right. And then the value will be 10. Now, if you're having any kind of confusion around the value of cards, we're gonna take blackjack as a starting point. So look up the rules of blackjack. What I'm going to do now is I'm gonna fill it in for the rest. But that's boring. And I don't want you to have to see it. So I'm gonna fill this out and then come back to the video when it's done. All right. Welcome back. There it is. I filled it in on Lee for spades. Here's where I want to show you how to work with bigger problems and turned them into smaller ones. First, I want to solve this card game for Just fades. All right? I don't want to do it for all 52. I just want to do it for spades and again, their optimal other ways of doing this. I just I just want to do something that's very basic. So we're going to start off with this, so I have all of my spades. There you go. I've speed, Stan. I have 987 all the way to two, and then I have the numeric values associated with each go up to 13 and then the value of the cards. So 987 all the way up to two. All right. Now, what would be step one step one would be displaying this. So let's go ahead and display it here. I was saying prints deck. So right here. I'm gonna go ahead and put in a putz card details. All right. Let's see what that did on this. There you go. So now I have my values for spades. Now, since I'm working with 13 cards only right now, I'm gonna change this. Decter 13 I can move this back to 52 later on. So if I run this, I have 13 cards. Right? So one all the way to 13 and then I have what each one are. So 1234 All right. See how a form is taking right now? So how do I associate or display these to be the cards, like ace of spades or King of spades instead of these numbers? How do I do that? First thing I imagine is I need to get a way of getting the card value associated with the numeric value. Right. So I have card value here, right here. 11 10 10. All of this. And then I have numeric value. So what is the card value associated with the numeric value, which I'm getting right now? Let's build a method to do that. So I'm gonna have a method define get card value. And so this is where we're actually going to stop this video. Because at this point, we've made a decent amount of progress. We have defined what it's going to look like we've been able to shuffle our deck and get values. Now, I want you to look up rules of blackjack if you're not familiar with it or any other card game like Bakara and look up values and look up. You know what? What exactly it is I'm talking about because I don't want you to be lost while we progress through this. All right, so take a look. And in the next video, we're going to start with our get card value method. All right, I'll see you there. 21. Build a game - continue adding features: along, Welcome back. So I hope you enjoyed learning about card games a little bit and the values associated with cards. So we're going to continue building this get card value method. And the point is, what do we want? What we want this method to do. We want this message to return the value off a card given the numeric value, right? So I'm passing in one of these 1 to 13 and I wanted to compare to this array or this hash within the ray, and I wanted to return the value associated with that card. So what are the things I'm going to pass in? I'm going to pass in, or this method is going to take as arguments. Ah, hash, which is like this, which is this one, right? And it's also going to take the numeric value. So a card. Let's say it takes in a card which is one of these 13 and then once it takes these two, it does something and then return to value. All right, so that's what I'm envisioning for this. So let's say card details. No need to complicate this. We can just leave it as card details and I'm going to That'll be the hash. And then the card, which is the card I'm going to pass in. What I want to see is if this card exists in this car detail, meaning this cards numeric value, which I'm passing in exists in this card details. I wanted to display the value. So how would we approach this first thing? I know this is Honore. This card details is an array. It is a hash of hash, but each element in there is the hash of a hash. But at the end of the day, it is an array, and each element is a hash. So I want to reiterate through this array, and I'm passing in card details. So card details dot each do, and then I'll just say name. So now I have, ah, loop where I can iterated through each of these elements in the array. Now, what do I do with each of these elements? Remember, each of them is a key value pair, right? And the key is the 1st 1 here, and then the value is this other hash right here. So I need to reiterate through each hash, so name not each because I've called a name. So each element is ah, hash, which is name and named out each Duke key and value. I'll just keep a key and value to, you know, stick to the basics. And so now I have another loop within here. Lips have, ah, incorrect spelling here. Do key value. All right, so what do I have? What do I want to do here? What do I want to do here? First, I want to return the value off the card here. So let's start with that. If let's say you know the numeric value that I passed in that works, I want to return value, but the value off value, right? So right here. Let's say I have key value pair. Let's identify the 1st 1 Let's a key key is fades to right, and then the value is this hash right here, which is numeric value 13 and value to so that is value. So now value at value this element, our value will give me too. So right here the value would be to the value at numeric value would be 13 and then the key would be speeds to write so that would be the key. So let's make an assumption that this is what we want to return. How would we go about doing it? So value add value will give me to When would it be that I want value at value? It would be if this numeric value of 13 matches what I've passed in as the card, right? Like from here, let's say I've passed in 13. So then Onley, then would it return the value of two? That's what I want. Let's just assume that we're gonna have a new variable, that we're gonna sign this value too. So we're just going to say new value. All right, this is a variable. We can call it anything you want and then compare it to card because that's the card that I'm passing in. So, Onley return this to if the value on passing in matches this value off this hash, right? So that's what's happening here. Now I need to define what new value is. How do I get new value? Simple enough. I have numeric value as the key value pair, and this entire hash is value, right? So I'm gonna say new value equals value, which is our whole hash here. Based on this do block key value. And then I want to assign in the value off numeric value, which is right here. So let's read what's going on here. Line by line card details on each do meaning this entire array, which is fine. So as we get through each element off the ray, new value is value at numeric value, meaning the new value on putting in is the value at the numeric value. So each time new value is first time is one second time is to start. Time is 34 times four and then the value that I want to return right here you can. This is difficult to understand. You can say something like this all right to start. But remember, this return is implied in Ruby. So better if you just eliminated, but just to keep it simple. Something like this is what I'm returning. I want to return the value on Lee If this, as we reiterate through the value add value, want to return if this new value is equal to the card number that I'm passing in. So 123 13 whatever. All right. So let's give it a shot. Let's try this out. Let's run this method. I'm going to quote all of this out. I don't need to print the deck. I don't need to do anything. I do need to shuffle. Actually, I don't need to shuffle right now. I just want to test out this method. So I'm going to say, puts get card value, and I want to pass in card details, which is my array. And I want to pass in value off 13 which is right here. So what am I expecting in return? I'm expecting to see 13. Right here. I want to see Spain's to show up. All right. I want to see this return speeds to. So let's see if this works with wrong this ruby e x three dot r b. There you go. That's too. Why? Didn't say too. That's the value to I just wanted to see the value, not the key. So the value is too. So let's try it with something else. Um, let's try right here. I have numeric value. Five. The values tan. It's spades. 10. Right. So if I give if I pass in five. It should return to him. So let's see if that works right here. I'm going to change this to five, and this should return tough. So Ruby E x three. There you go. 10. That means they returned the correct value associated with the card. Let's talk. Let's try another one. I want to try out Queen of spades. Numeric values. Three. So if I pass in three, I'm expecting the value to be 10 as well. So right here, let's say I want a pass in three. Let's do that and then run it. There you go, 10. So it's working right? So now I'm able to get the card value associated with a number that I'm passing in, which is great. So now I can shuffle a deck. I can print to the deck, right? I don't need to put the car detail, which is this entire array, and then I can pass in or get card values as I need. So what would be the next step? The next step would be for display purposes. I want to get the card type as well. What I want to do is right here. Spades, two spades, three for display purposes. I want to display that as well. I don't want to just display the number or the value. How do I do that? So let's build a method. We have get card value. So I could have something like this. Get card tight, all right? And it would be very similar card details. And then I want to pass in the numeric value and get the card card type associated with that value so I can display something like, you know, you have the king of spades or queen of spades instead off just a value associated with it . And it would be very similar to this. I'm simply gonna copy this whole thing. Come and see Command V. All right, so here I have card type says card details on each do name. That's fine. So it's gonna generate through each element in the array and then named out each do key value. That's fine as well, because I want to iterated through each key value pair right here and then new value equals value. Numeric value that's also the same was just fine. Now what do I want to do in the last line? the last line will simply be instead of value. I want to say return key, right? Because right here I want to return the name, which is the key. So return key. If Sorry, a mistake there. If now watch you the condition be. I want to return the key if the new value which is right here equals numeric value. So it was a new value here equals a numeric value that I've passed in. Then I want to return the key. So that's it, right? So what will that do? Let's test it out right here. I have puts get card value card details, and I also want something like this. Put gets card type and I'll pass in card details as well. And I want to pass in the numeric value off three, which is the same for this one. So this let's see what the answer should be, Right? We have three and three. So the three right here value is 10 and queen of spades. So I'm expecting it to print out value off 10 and then Queen of Speeds after a dozen shenanigans right here. So let's run it. There you go. 10 and queen of spades. Perfect. So now we have methods that return the name methods that return the value. And we have a way off shuffling the deck. Perfect. So in the next video, we're going to start building how to play this game, right? We don't even know what the game is yet, but we have values. We have keys. We have names. We know how to shuffle. So I think we have enough to get started with a game. So we'll do that in the next video. I'll see you there. 22. Complete the game: Hello and welcome back. So we're at a place where we can shuffle, We can get card value and we can get card type. So now let's make the game. First things first. We have 13 cards. The first car goes to the player. NextCard goes to the dealer, which in our case, is a computer. Then the card after that goes to the player again. And then the card after that goes to the dealer, right? So that's how it works. So how do we get these four out of the deck and get them permanently? How do we do that? So that our deck no longer has these four in them. We do that. If you remember. We have that method in a race called Pop, so let's do that. Let's build that part first is I'll get rid of these two because we don't need them. Player card one equals and are deck are shuffled. Deck is called deck, so dek dot pop Remember, the pop method is going to get the last one and then return it. All right, so we have Claire card. One deg dot pop than computer card one equals dek dot pop now deck. After this first execution happens, seven from here will be removed. Then it's gonna be three. So when computer card one equals decked out pop three is going to be assigned to computer card one, and then three is gonna be removed. And then I need player card too. So player card two equals dek dot pop again. And then computer card two equals dek dot pop. So now player has two cards. Computer has two cards, right? How do we display this? Let's go ahead and display it. We're going to say put player is holding and then player card one and player card too. Right? Have to close this out. That should be good. And then I'm gonna say dealer is holding computer card one and computer card, too. Let's see if that works. We have a problem here. Computer card one, uh, was string there. This is why text editors air so great. Because you have pointed it out. And I had a syntax error here, and I was able to identified right away that there was an error there. All right, so we're gonna run ruby e x three dot r b. Check this out player is holding 11 and four. Dealer is holding nine and 10. All right, so let's run this again. Now it's different. Player is holding because it was reshuffled players holding 12 9 dealers holding three and seven tries again. Perfect. So our mechanism is working right? This is great. But what is the problem here? The problem here is these are the numeric values. They really have no meaning beyond to this array that we have already arrangement. So we need to get the card values associated with the numeric values. And not only that, we want to display what the cards are. And we also want to add in totals for the card. Right? So let's go ahead and do that. First, we're going to create this method called hand value, so define hand value. And then what do we want this method to do? We want this method to be able to give you simply the total off two cards from a deck. That's it. So I'm going to pass in card details, going to pass in card one on, then card, too. All right. And then hand value one is going to be get card value. So I'm calling the method card value. I want to say card detail, since I need to pass that in and card one. So this gets the value associate ID with card one and then hand value to equals again, get card value. I'll pass in card details. Now, you may be wondering what the hell is going on. There's all this extra code, all this reputation and you are 100% correct. Again, this is not optimal code. I want to walk you through building something, and then basically re factoring the cold later on are making it look better later on. So I want to give you the raw code of how something like this you can come up with and then make it nicer later on. All right, So card, too. So now I have hand one, and it can get that, um, you can get the value of card one and then the value of card, too. Now, what do I want to return? I wanted to return the hand value associated with these two cards. So now I have the values of each card simply. I want to return hand value one plus and value to. That's what I want this method to return groups. There you go. So what does this method do? It takes in the deck. It takes in two cards and it returns the hand value associated with those cards. Simple enough. So let's make it work. Let's make it work in our running off this game that we have going on. So Player one is holding player card one. If I run in right now, Player one is holding 11 and eight. We want this to not just show 11 and eight. We wanted to show the type of cars here, so let's update that we know we have a method to get card type. So within here, I'm going to say, get card type. And what do we want to pass in? We want to pass in, Are all right? So card details right here and then player card one. And what will that do? This will display right here. This will display the card that I'm holding a type of card instead off the value associated with it. But I'll still have the value because I have player card one, so I can use that to get the value with our method later on. So right here, I'm going to change this to same thing, get card type and then card details and player card to write. Similarly, I want to do it with this computer card one. So get card type, and I'm going to say card details. And this one is going to be computer card one. There we go. And right here again get card type on and we're gonna say card details and computer card, too. So what I'm expecting this to do now is I'm expecting it to display what the cards, dealer and player holding instead of the numbers. So let's see. Let's try this out. Player is holding eight of spades and queen of spades. Dealer is holding nine of spades and seven of spades. Perfect. All right, so better display. Now. What do I want to do? The last step? Remember, I have this way of getting hand value, right? So I want to display the value of the hand that the dealer and player holding right now. I know it's eight of spades and queen of spades, so this should have been 18 and nine and seven this should have been 16. So dealer should have been holding 16 players. Should have been holding 18. So how do we do that? We have our method to get hand value, so let's just run it right here. We're gonna say and value is pound. We're gonna say hand value, then passing website of two when a passing card, details and then player card one and player card, too. All right, There you go. Similarly for the dealer right here. And value is hand value. Se card details, computer card one and computer card, too. Whoops. I have an extra one there. All right, Close race. Save that. So let's run it. All right, so that's a problem. My screen seems to have frozen. So what I'm gonna do is I'm gonna open a new terminal. No terminal. It's reconnecting. Probably a connection issue somewhere so that this work itself out. I'm going to be back as soon as it's done. A pause. The video temporarily. All right. It looks like it reconnected. So But I'm in the wrong directory. I'm gonna move outs. Let's see Ruby projects. And then this is e X treats a ruby x three Darby. There you go. Blair is holding spades three and spades five, and the value is 85 and three is eight. Dealer is holding nine of spades and eight of spades. The value 17. Perfect. So there you have it. Now you have the mechanism in place literally build a multitude of games that you want. So here is your homework assignment. All right. Homework assignment is take what you have here. Add in a loop. See, now, if I run this, it runs it once. But you know how to keep this running in loop, meaning? Shuffle it again. This entire part Put it in a do loop and ask the user if they want to keep playing. And then if they say yes, you keep shuffling and then keep assigning the cards. Now make it as complex as you like. Complete a blackjack game. All you need to do for blackjack is to ask the player if they want to hit again to look at 2 21 Obviously, it adds a little bit of complexity, but I totally think you'll be able to do it now that you have the mechanism in place, make any game you like and then post a solution to the Q and A section. Alright posted title it your completed game and then posed a solution. And I really am looking forward to seeing what you come up with. All right, good luck. And I'll see you in the nice video. 23. Object Oriented Programming - An introduction: Hello. Welcome back. I hope you enjoyed building your game. Now, in this video, we're going to start object oriented programming and I'm going to stick to the ruby and rails principles off. Don't repeat yourself. So I have Ah, very thorough introduction to object oriented programming from another course of mine. And the background will seem a little different. It will be a darker theme. It's done using nitrous. So I'm just gonna move on to that and give you the introduction from there, and literally everything else will remain the same. You'll run it from the terminal here. You'll create and work on your files here, and you have the following three. Over here is just a background will look a little different. All right, so let's open the door to object oriented programming. Now. What is object oriented programming? Object oriented programming is a programming paradigm that uses objects and their interactions to design and program applications. What does that mean? What it means essentially, is that object oriented programming allows the programmer to block off areas off code in the application that perform certain tasks independently off other areas in the application . Okay. And it manages this by using what's called objects. Let's take a second to think about it. Taken application. This large enterprise level application. You have so many features, methods, procedures, variables, all interacting with each other. Now, if you're unable to section off areas as independent of one another, then you have one massive mess. Since dependencies would be impossible to deal with, you can update one item in one small part of the application, and that would have massive ripple effects throughout the rest of your application. So object oriented programming was developed to deal with these kind of complexities. Let's look at the core off. What object or innate programming is. We'll talk about classes first. Now what is a Class? A class is essentially a blueprint that describes the state and behavior that the objects off the class share. So, for example, you could have a class off user and then you would have methods within that class that all users in your application would share. Okay, so that would be a class Now. Class can be used to create many objects so you could have many users, and objects created at runtime from a class are called instances off that part circular class. So basically, let's say you have a user and a user signs up for the application. That user is going to be created based on an object created from the user class. Okay, and how that's going to happen is every class has this initialized method, and it initialize, is the user based on attributes you provide. It, for example, could be named email or anything. And then the program uses that object to create the user or signs in the user or anything associated with that user, or updates the user or any other function that you want to perform on the user. And then that, basically, is the lifespan off that particulate er user object. Now let's look at some basic definitions off what constitutes object oriented programming. So abstraction, abstraction is what we just discussed. It's simplifying a complex process off a program basically modeling the program, which could be an enterprise software solution or any other solution by modeling classes appropriate for it. So you're basically grouping behavior in classes. Okay, One of the most important parts comes next, which is inheritance. Inheritance is used when a class inherits behavior from another class, and the class from which behavior is being inherited is called the Super Class, and the class that's inheriting the behavior is called the subclass. Then you have polymorphism, which is when a class inherits the behaviors off another class but has the ability to not inherit everything or change some of its inherited behaviours. For example, you could have a method in the super class, but that same method could be defined again in the subclass, and the behavior could be changed. Okay, And then there is encapsulation. Encapsulation is the concept of blocking off areas of code and not making it available to the rest of the program, so other objects cannot impact that part of the program. All right, so let's look at this in a diagram. So here you have a user class, which, as a rails developer, you're going to be dealing with a lot. So you have the user class and the user class can have multiple classes that subclass from it so you could have the seller class. You could have the buyer class. You could have the administrator class all these classes subclass from user class. So if user has a method that says Run, then seller will inherit that run. Method buyer will also inherit that run method Administrator would also inherit that run method. So when you have an object of buyer that object, you can say whatever the name of that object is docked, run, and that code would actually work. Now, any of these classes, you can take that run method and define it again within the class. And that will actually run the method that you define within the class instead off the method that's defining the user class. That is what we briefly spoke of above, which is polymorphism. Okay, now let's take a look at some examples, which will be much easier to understand. So let me pull this up. So in my terminal, I'm going to navigate over to my Ruby projects. See the code Ruby Projects. Okay, now we're here. I'm going to create a class so new file. I'm going to call it user dot R b. Pull it up Now. In the definition of a class, you're gonna have to use camel case, so the first letter will always have to be capitalized. So, class, user, and there's my class. Now let me say this now that literally is my class. Okay, so how do I initialize an object of this class? You can initialize an object by using the doc new keyword. So let me say user equals capital. You user, cause I'm calling the class doc new. And then I want to simply print out my user. What does that do? So puts user. Save that. Let's run it. So I'm going to say Ruby User, don R B There it is. There is my user object. Okay, now there's nothing going on in it. I haven't initialized anything, but my object got created, and you can see it over here. It says user, and there's object. Okay, Now that object is long gone. If I run it again, notice how now it's a different object. Its life cycle is over. Basically, because I'm not doing anything with this object, I'm literally initiating it. And then that's it. Now let's look at a little bit of inheritance. All right, So what I want to do here is I want to take my user class and see what it where it inherits from. So if I simply do this say puts user that ancestors, now that ancestors is a method, Okay, it lists out all off the super classes off user when I save this and then run notice how user user class, which I just created some classroom object with some glasses from colonel with some classes from basic object, which is the basic off all ruby coat. Now here is something important to keep in mind. Everything in Ruby is an object. Everything that we've been working with, strings, integers, everything and Ruby is an object. Now I hope that concept is getting a little bit clear. All right, now, let's give our class a little more meaning, All right. So we have users. I want to give my user a name and an email. So first it's the key initialized method. All right, so define initialized. And basically the initialized method gets called every time you create a new object. So if I want to give my user and name, I do this name. But what do I want to name my user? I'll say name, But this means I have to pass in a name so define initialized, and I'm going to say name So when I create user Dot knew, I'm going to pass in a knee and then that name will be the name off my user object. Now this After what is this sign? This signifies that this is an instance variable. This instance variable exists as long as the object exists. So whenever you see that an object gets created worked on now during that life cycle of that object, we're going to have the name off that object stored in this instance. Variable. Okay, so that is an instance Variable because it exists within the object. Instance off that class. Okay, so let's go ahead and create some new users. So now we can say user equals user dot knew and I'll give it a name. I'll say mature and then I'll create another user user. One equals user dot Knew I'll say John, and then use their two equals User dot knew and I'll say, Kevin, I'll save that. Now Over here, I'll get rid of this line that says, user, that ancestors I don't need that anymore puts user put to use their one puts user to Let's see what that does me saying that check it out. I have three users created three different user objects. Okay, now, if I pull up the diagram that I was referring to earlier, right here, notice how I have a buyer Object, John, and then I have another buyer. Object net. All right, so now have to objects of the same class. So what can I do with these objects? Let me define a method. All right, so I'll say, Define Run, Bend. And then within the method, I'll just say, Hey, I'm running. Save that now. Over here I have my three users and I have this method. I have this instance Method. So can I make my users run? Let's try it up. I'll simply say, user docked Run user. One doctor Run usar to Dr Ron. Let's see what that does. Say that. Check it out. It's running, and it lifts it out three times because each user is running. That means all three of our objects have access to this instance. Method run. That's part of the user class. And that shows you that each object that's created from the class can use the instance methods within the class. Okay. All right. Perfect. Now moving on. What if I want to display the name? See, I have these three users and I have the objects, right? Mature John and Kevin. But it's not showing the name. What happens if I simply referred to the name? Let's see. Let's try it. So I'm going to get rid of this, all right? I want to simply say user dot name. So it should say, mature. Let me see if that fire on it. Undefined method name for user. And it's showing that I don't have a method for this. This is an air, right? What if I say user got acne, which is basically the instance Variable. What does that do? That's a sin tax there. Okay, so this is obviously not working. So how do I get the name? In order to get the name, I have to create a method within my instance to get the name. All right, so I'll say, define, get name, and that simply returns name. Okay, so now let's try that. If I type in user dot Get underscore name save. Now. Let me run the program again. Whoops. I didn't I'll put it so. Puts user dot Get name save that run. There it is. Mature. So that is a getter method. Okay, A getter method is basically when you have access to an instance variable within the class four, an object. So here I have the user object and I can get access to the name off that object by using this getter method, which is getting me now. What if I want to set Name? What if I want to change the name off my user so I'll write another method they find set, Underscore name, and then I'll set it to name. All right, I seem to have a problem here. My container stopped working. Now you may face this error every now and then with nitrous, so I'm just going to click on back to dashboard. Okay. I'm gonna start my container. Stop. So started, and I'll return once it starts. Okay, It's back and running. I'll click on this, which is my container. Start, then open. I d e. Okay, we're back. So seedy code Ruby underscore projects. All right, so over here, back to what I was doing. Defined set, underscore name equals and I'm going to set it to name and then and And what I want to do is I want to set the instance variable name to whatever name it is that I pass in. All right, so save that. So over here, I'm creating a user mature lemon, get rid of these two. All right? So, user, not new. Mature, but user dot Get name. Now. I want to change the name. All right, So how do I do that? Let me say, user, not set name, which is the method we just created, and we'll set it to, let's say, John. All right, Now, if I do puts user dot get name to display the name. Save that. Run this. So ruby user dot R b mature, undefined methods. Set name we think to have a problem. Okay, so right here, I made an error. This is wrong. Syntax over here, it's going to be set. Name equals John. Okay, I'm setting the name to John, so I'm going to save this and then run it again. There you go first. If my sure. Then I'm saying the name to John now. It's John. Okay, Now what we did right here with these two methods are established getter and setter methods for my class. Okay? And Ruby makes it super easy to do this in one line without having to set the getter and setter methods because it is so common. So what I'll do is I'll get rid of all this, all right? And simply on top, I'll say 80 tr underscore acts say, sir. Okay, so 80 tr underscore access er and I'll say name. And just by typing this one line, what I do is give my user class the getter and setter methods for name. So over here instead of get name, I'm just going to say, user Doc name, then over here. Set name. I'm just going to say, user got name and again, what's user God name? Save this, then let me run it again. Check it out. It works perfectly so that some ruby magic for you. Now let me add email also so lad email than in the initialized method. I'm going to pass an email as well, and then I'll set email instance variable to email, which we passed in and over here user dot new mature will be the name that I pass in and then mature at example dot com will be the email that I pass in, so puts my users. Name is pound open brace user dot name, and his email is pound open brace user email. Close quote, then user dot named John. Let's update the email as well. These are not email will set it to John at example dot com now puts user name. I'm going to change this to my users. New name is Pound user dot name, and his email is pound open brace user dot email. Let me say that. Run this okay, My users name is mature and his email is my shirt. Example dot com misers. New name is John on his email. Is john at example dot com? Okay, perfect. So now we've learned about classes about object oriented programming about objects. The initial I've method attributes Successor, which gives you the getters and centers, and you were able to assign and then update the name and email attributes off your user by working with instance variables and the user object. So that's a very good amount we've covered in this video, and in the next video, we'll look at some or interesting things we can do, including working with modules. I'll see you there 24. Inheritance and modules: Hello and welcome Back In the last lecture, we briefly spoke about inheritance. Let's look at it in a little bit more detail. So we have our user class, which we've been working on. And if I pull up the model that we had based this on right here we have the seller, buyer and administrator classes which subclass from the user class or inherit from it. Okay, so let's go ahead and build this. How do we do that? First, it's over here. I'm going to say class buyer and then left Arrow for subclass from user. Okay, Similarly class cellar subclass from user and then class admin subclass from user as well. All right now remember how we have this method that says Run? So let's create a couple of objects based on these classes and run the run method on them and see if it works. Okay, now, over here, I'll leave, is there? I'll say buyer one equals buyer. Dot knew and I'll say John Doe and then John Doe example dot com and then I'll say buyer one doctor on. Let's see if that works going to say ruby user, not RB. Okay? See how it says, Hey, I'm running. That's John Doe. Okay, so I was able to inherit into my buyer class from the user class. The instance. Method run. Now what if I want to change this method? What if I want to change how the buyer treats the run method, Whereas I want to keep the seller and admin using the same run method as the user? How do I do that? First, I will create two more buyers, so I'll paste it twice. I'll make this cellar one equals cellar dot Knew John Doe one John Doe one. And over here, I'll say admin one admin dot Knew John Doe to John. No to. Okay, Now all three are going to run. If I run in the first time. It says, Hey, I'm running three times. Now, let me go ahead and update this method within buyer. So if I do define run, end and save puts a I am not running and I'm in buyer class. Save that. Run this. Okay, so all three of them are showing this. Okay, I know what happened. If you look back here, I've written seller one, but then I'm making buyer one run. Have to update this to cellar one. And I have to update this to admin one. Save that. Let me run it again. OK, notice out. Now the buyer is the only one who's running and saying that I'm in bar class, whereas the other two are simply running. Okay, so that's inheritance for you Now. One last thing I want to do is I want to show you the ancestors. So if I type in, puts buyer the ancestors, save that run this notice how buyer, then its user, then its object. So the hierarchy updates. Remember, user had object, Colonel. Basic object. We'll hear buyer has user before object. Colonel. Basic object. All right, Now let's look at one more thing for classes before we move on to modules. And that is a classmethod. Okay. So far, we've been dealing with instance methods which, for example, are here We have run here. We have run also. What is a classmethod? Ah classmethod can be called directly on the class without in Stan shih ating Any objects to have this a run method run we have to in Stan, She ate or initiate an object. Right? We have to create a new object and work within that for a classmethod. We don't need to do that. We can call it directly. For example, here we can just say define some classmethod and end The way you call a classmethod is you say, self docked and then the method name. So we'll just call it identify yourself on you can say, but a I am a classmethod Save that. So let's run this. I'm literally going to call the class directly, so I'll say user dot Identify yourself. Save that. Let's run this. Check it out. Hey, I'm a classmethod. I was able to do it without initiating any objects. All right? And if you don't believe me, I'm just gonna get rid of all of this. See that? Run this. There it is. Hey, I'm a classmethod. No objects yet. I was able to access a method within the class. Now that's a classmethod. Okay, All right, now, moving on to modules. So what is a module? A module. It's similar to a class, but it basically is a collection of behaviors that other classes can use via mix ins. Okay, so you have to include the module in the class that you want to use, but you cannot have an object off a module. Okay, you cannot initiate an instance variable off a module or anything. A module has to be mixed in to a class, and then you can use the methods in a module. So let's take a look at it real quick. So let's say we have a module and will say destruct herbal. Okay. And the If you're wondering why the able it's because by naming convention modules, if possible, would be named ending with enable. So if you had a module for walk, for example, you would have it named as a walkable. All right, so module distractible going to say define, destroy. And then I'll give it any object and say puts I will destroy the object. Okay, Now, if I want to use this method that this module destruct, Herbal provides over here. Let's say in my user class I have to include it. Otherwise, I won't have access to his destroy method. So let me create a new instance off user so user equals user dot new name, I'll say assure and email. I'll say I'm a sure at example dot com. Now, if I try user dot destroy and I'll say my name. Save that. Let's see if it works undefined with a destroy for user. Okay, so this method is not accessible by user. But if I do something like this, I just add include destruct herbal. I have, ah, error there. Destruct. Herbal. Save that. If I run this again, I will destroy the object. There you go. Now you have access within the user class to the methods in the destructive will module. All right, so you can have multiple methods in your module, and then as soon as you include that module within your class, you have access to all of those methods, and then your objects will have access to those methods as well. Okay, great. Now, at this point, I would recommend you look up some methods, classes and modules and Ruby by visiting the Ruby dash lang dot org's site and just read through some of them and try understanding as much as you can. OK, don't spend more than 30 minutes to an hour and try to make notes of what you've read, just so you could refer back to them at some point and and also that you remember what you've gone through. All right, now, welcome back to 2016. I hope you enjoyed that intro and then working with inheritance, an object ordinated programming in general. Now we're going to obviously go in much more detail here. So starting in the next video, we'll start applying some of what we've learned. All right, so I'll see you in the next video. 25. Build a file reader class: Hello and welcome back. So in this video, we're going to learn how to read in contents of a file on display it to the screen, and we're gonna use the console to do this. So right here I have created a folder called OPI Underscore files, and you can call it whatever. You know, I initially called it file ops. Since we're gonna be learning how to work with files in this section. So go ahead, create a folder under Ruby Projects, and I'll wait. All right. I hope you are able to do that. If you weren't simply right. Click on Ruby Projects. New folder would have done the trick. Now, within Opie files create this text file because the first thing we're going to do is want to learn how to read in the contents of a simple text file and then display it. So go ahead and create a file within Opie files. I have this. I've named it sample. Doc text is ah, text file. And I just put in contents. This is such a beautiful day. I wish every day was so nice and sunny. So we want to build a class where we can pass in a file like this, and it will simply read and display all the output to us in the council. All right, so let's go ahead and build it first thing. I'm going to create a new file. I'm going to call it file. Underscore reader dot r b Pull it up. So first things first, I have to give a class. So class file reader. Now, if you remember, the name has to be camel case. So capital F, file capital. Our reader and then end. Okay, there you go. We have a class. Now. What do we need to do? First, we need a method to read in Ah, file. So let's go ahead and define a method, so I'll call it read, and then I'm going to say, puts reading file using simple file reader. Now, how do we read in the file? Let me create a variable. So I'll say contents, and then I'm simply going to say file So capital F OK, file dot Reid. And within it, I'm going to give it the name of the file sample Dark text. All right, Save that. So let's run it. How do we run this. If I simply do ruby file reader dot r b. What happens? Nothing happens because I simply have a method and I have a class. I'm not initiating a new object of the class, and I'm also not calling in the method. So let's do that. I'm going to say X equals file reader dog new and then X starred Read right. Say that because I have the read method within it. So let's run it again. Reading file using simple file readers. So it displayed this, but it didn't display the contents. Why didn't display the contents? Because I didn't have ah puts here or here, so it's not displaying it. Let me go ahead and at that, allowed it here. I'll simply say, puts x dot reid reading file using simple follow. Either this is such a beautiful day. I wish every day was so nice and sunny. There you go. Now, if I pull up sampled up text there it is. So you have a file reading mechanism. All right. Great. Now what are the few things here that we can improve? First of all, I want to initialize the file reader with the file name right here. I don't want to manually input this for that. I need to pass that in, right. So I'll leave the key initialized method. So I'm gonna enter that here, defined initialize apps, initialize, and I'll initialize it with file, that all passing and so I can use it throughout my program at file equals file. So now my father reader class will be initialized with, ah, file. But if you remember to make this work correctly, I need my attributes Successor, So 80 tr access er file. All right, so now I can replace this instead of hard coding it. I can simply run this with file. But what will happen if I run this? Now? Let's run it. Okay. Initialize wrong. Number of arguments given zero expected one. Why is that? Because I'm expecting to pass in the file name. And I haven't done that here where I'm creating a new object of file reader. I'm not passing in the file name. So if I type in Sambo whips within quotes, sample doc text Now let's run it to see what happens. There it is. Now it's working. Perfect. Now next up. What if I don't want to have this run from within here. What if I want to read in the following from the command line when I'm running this? What do I do that here? Remember, you can refer to this as art, right? So simply a R g V and open square bracket. Zero close square bracket, close parentheses. What will this do? Let's run it. If I have just run it now, it's going to say there is a problem. But if I run it now and give it sample dot txt There it is. It's working now. Perfect. So see how incrementally making this better and better and making it do things the way I want. That is how you would go about building something, build a basic structure and improve it as you go along. Now, let's come to one of the key aspects of object oriented programming, right. So here I have a file reader and I have a class file reader, and it's nicely neatly packed over here. Why do I have this running code within this file? That doesn't look good. It just feels like it doesn't belong here, right? So I'm going to create a new file and call it runner. And then from there I will do all my execution code so the classes are neatly packed in like this. So without any execution code, right, I'm going to create a new file right here in this folder. I'm going to create a new file, and I'm going to call it file operator dot r b. And then I'll copy these two lines from File Reader Command X Goto file Operator Command V command us to save it. And I have to save file reader also. So now notice followed or only has the class nothing else. No execution code. Now what will happen if I run this? What if will happen if I run file operator Darby, Let's try it. I'm going to Iran and using the same thing. Nothing happens. Why did nothing happen? I ran it and I gave it. Whoops. I ran file create file reader there. So let's run file operator file operator dot R b, and then I'll give it sample text on initialized constant file reader. What happened? This cannot find file reader this class because it's not in the same file, so but it's in the same directory So how do we essentially get our runner program to find this file reader RV class, since it's in the same directory. What we can do is this we can say require relative and then within quotes file reader, You don't need to put in an RV or anything like that. It assumes that it's there. So now it will find this. Let's run it. There you go. It found it. It ran it. Now my runner is working perfectly. You don't take my word for it. Let's create a new file. Let's create a new file and we'll call it Sample one doc text and then sample one dot text . I'll say this is ah, test. Save this and there's my file operator. So there you go sample instead of sample, I'm going to say sample one doc text reading file using simple follow leader, this is a test. There you go. See how easy that Waas instead of making updates to quote anywhere, I just change the file name the and it ran perfectly. Now we can go one step further. I don't like this puts over here, so I'm gonna get rid of this and remember this is Ruby, so I can simply chain this. So, Dr Reed, I don't need the X anymore. Over here. I think we want to create a new object of the class with the argument as the file name. And I simply want to read it so I don't need this either. But what will this do? Let's see what it does. Run it. It just ran the puts it in. Give me the contents of the file. Why is that? It's because if you look at file reader, I'm not output in contents before I was using puts over here. So instead of this in the read method, I'll simply do put contents, say that and then I can rerun it. There you go. There's a test. Now, if I want to run it with sample and set a sample one. There you go. Now we ran it for that file. If I want to run it for both, all I have to do is copy this pasted there and then this to one. See that? Let's run it. Sampoerna in Sample one, Doc text See it? I'll put it both it are. Put it sample first and then sample one. All right, That's great. So now we have a file reader class, and you're able to read in input from text files and will continue working on this in the next video where we'll learn how to work with CS V. Amel and Jason Files. All right, I'll see you there. 26. Build CSV and YML reader classes: along. Welcome back in the last video, we learned how to create Ah, file reader file operator and work with reading and out putting the content of a text file . In this video, we're going to start working with C S V files. If you don't know what C S V is, see SV's comma separated file and it's pretty commonly used all over. So I'm going to first create a new file, and I'm in my OPI Files folder and I'll call at CSB Reader. See SV underscore reader dot r b and this is going to work with CIA's We files here. I'm going to say Class CS v Reader. And so there's my class definition. Now let's create a sample CIA's V file within this folder. Create a sample CSB file is the one I have. I just basically put in a comma space comma space after each word. And this is the contents of my sample. See, as we felt, you're obviously welcome to do whatever you want here and create your own version. All right, so our aim here is to read, and I'll put this file so first up is we're going to create a read method. But before we do that to work with CS V Files in Ruby, you need the CSB library from Ruby. So on top, I'm actually going to code this out for now. Okay, You need to do this, require within quotes. See SV. Now we can work with CSB files. OK, so let's first outside of the class create a method to read in the file so defined read so that we don't make it too complex in the beginning. And we want to say something like reading in C as we file similar to what we did with our file reader leads do contents equals See as v dot reid o K C s v is the class, and we're going to say sample dot c as we read in that file, and then we want to print out the contents off it. All right, let's see what that did. We're gonna say ruby CSB, underscore reader dot R b. It did nothing. Why? Didn't do nothing? Because I didn't call this method anywhere. Right? So I've just called this method read. Let's do it again. Oops. There you go, CSB reader. There it is. The words in this file or a comma separated it. I'll put it the file. All right. Great. So we have this mechanism working. Now, let's turn this into a class. So go ahead, pause this video, convert this to a class for CSB reader using the same methodology we used in the file reader. And I'll wait. All right. I hope you manage that. So first things first we have to do right here. We're going to say Class C as the reader, right? So it's basically what we had here, and then I'll get rid of this. Read command an end. There you go. So now I have ah message within my CSB reader class that is read to run this. What do I do? I simply do X equals C as the reader dots new and then X docked. Read Because I have the read method. Save that. Run it perfect. It's working. All right. Now, what's the next step? Next step is to use the initialize method. So I'm going to say define initialize with file because I'll pass in the file name and then dot at file equals five. All right, so right here, See SV dot Read. I don't need to do this anymore. I can simply say at file, so I don't have to manually update this. But now to run it, I have to pass in the file name. So here, I'll pass in the file name, See as V. There you go. Let's run it. Perfect. Now it's working. All right, now here. We're going to use something. We've learned something we've learned, but not really used so far. That much, which is inheritance. Remember, when we're introducing object oriented programming, we learned about inheritance. Look at this initialize method right here. This is exactly the same code we have in file reader, right? So why do we need to re use this? We don't. We can simply inherit from file reader, so CSB reader will automatically have this initialized method by default. How do we do that? Let's get rid of this. All right. But here we need to say, require underscore relative and then file reader. So what we're telling Ruby is to look for file reader and it should find it. Now we want to inherit from their right. So to inherit left square bracket were going to say file reader. So now see SV Reader by default has all the qualities are file reader in that means the initialized method. If I run it, There you go. It's running again. Perfect. So now again, remember, there's a class we want to keep it nice and clean. Let's get rid of all this execution code from here. Save that. Let's goto file operator now. I don't need the 2nd 1 at all, so I'm just gonna delete that. Let me call it out the 1st 1 where I'm reading from file reader and then I'll say, See, as we reader Doc New and then they are GV and I'll pass in the argument from command line all right, and then close parentheses and I want to run the read message on it. Let's see what happens right here. Ruby Nazi as V reader anymore. I'm going to say file operator dot r B and I want to run it with sample that CIA's we all right. It says uninitiated ized constancy as v reader. Why did that happen? See, I did not require it here, so I need to have a reference to where Ruby can find CSB reader so require relative and within quotes. See SV reader. There you go. Let's run it. There it is. Perfect. Exactly what we had expected. All right, We're getting pretty good at this. So now we have a file reader. We have a file operator. We have Ah, see, SV Reader. So now we can read in text and see SV. What about? Yeah, Mo. So this is what I yam Oh, file looks like sample dot Yeah. No, I have it. This file is structured hierarchically. Okay, so go ahead. Pauses video for a few minutes read up on Yeah, mo files. So it ends with dot Why ml as the extension and I'll wait. All right, Welcome back. So let's go ahead and create this file. I'm going to close out some of these other files I have here while operator can stay open File reader out those out. All right. So go ahead and take a shot. Based on what we have learned from file up file reader and CSB reader create a Yamil reader . All right? I hope you mount. Is that so? I'm going to create a new file. Why am l? Underscore reader dot RV So how do we want to do this? We already know that we're going to need something like this, right? Require relative because I'm going to inherit from file reader that I know already. File reader And then class. Yeah, mole reader subclass meaning inherits from file reader. So I don't need to initialize class anymore right here. Okay, now what is missing here? Just like we did on the CSB reader require See SV for yamma reader. I need Rubies. Yamma library. So I'm going to do require within quotes. Yeah, well, close quote. See that? All right now within it, I need my method so defined read and and I'm going to say puts reading file using Yam o vile reader. All right, And then I want to do the same thing I did before contents Equal yam Oh, here, docked load underscore file. And if you're wondering where I'm getting these methods from like yeah, McLeod's giving me this method or see SV clouds giving me the read method. Take a look at Ruby Documentation and Ruby Dash lang dot org's site. Okay, All of these are listed there and what they do, I'm just showing you what we need for this course. But that is how you would look it up. You would go to Ruby dash lang dot org's and then look up. What classes and libraries you have available to you. Or even better than that. You can google it. Or you can also look on stack overflow and you'll find tongues of results of how to do these things. All right? And then I'm going to read sample dogs. Yeah, I'm all say that puts contents. All right. Now, if I run my file operator, what's gonna happen? Let's try it out. I'm going to quote out the CSB reader, and then I'm going to say Yeah, Mo reader. Is that what I call it? No, I call it Why am l so yeah. Mole reader dot Knew and then r v zero dot reid over here. I'm gonna need to require it. So require relative. Yeah, I'm all underscore, reader. Let's take one last look at our file. I'll reader Whoops. I should have file here. Okay. So time to run it when I run it using file operator and then sample. Not CSB. This is Yeah, Mo. Okay. Reading file using Yam. Oh, file reader, this file is structured hierarchically. Okay, so it's working. Notice the half structure for Gammell Falls. That's pretty much how Yamma files are structured. Now, that's what I wanted to cover in this video. That brings me to the homework assignment. I've showed you how to now work with text files, CSB files and YAMMA files falling the same approach. Create a class four, Jason reader. Okay, create a method by which we can read in a Jason file and output the contents to the screen . All right? And Jason again, if you don't know what it is, look it up on Google, all right? And that is your homework assignment. Good luck. Please, please, please post your code to the Q and A for this discussion and say, Here is my Jason class. I don't need to see the file operator. Just make sure you post the Jason underscore reader dot RB file on the contents there. All right. Good luck. And I'll see you in the next video. 27. Automate reading file extensions: all right. I hope you're able to complete the Jason Reader without any issues. This is what my Jason reader looks like. I have this have this additional line where I put the contents in Jason format, and that's simply so I can get ah, hash rocket type form. There it is. You. I get this form, which I like. Okay, so if I look at file operator now, that's what I did. Jason reader dot Newark zero Read Now, one thing you'll notice here with all of these it's essentially the same thing, right? Follow leader that New York Zero not read. Why should I need to do this? Why should I need to identify this when you know we're programmers? So we want to auto meat and find out which class to use. Based on the extension of the file. I want that automated. For example, have C s V. I have Yamil have Jason. I have text. So based on the extension of the file, I want my program to use the appropriate class because now I have four classes. All right, so that's the next step. And this will require some ruby code. So let's get started, and this again is putting some off some mawr off. Why object oriented programming is important. And the strategy that we used for this program, right? Basically, Now, in order to run this, all I have to do is work with the extension on the rest because there, neatly organized in classes, will work smoothly. All right, so let's get started. So first thing we need to do is we need to identify the extension. I'm gonna comment this out, and I'm going to need a method. So define vile extension is what I'll call it, and it's going to read in, Ah, file name. And I simply wanted to display the extension. All right, How do I do that? You can use the split method, so I know this is going to be a string the file lane, So I can do file name dot split and the split method. I'm sure you've seen this already. Well, when we were experimenting with strings and methods on them, this will split based on whatever I give it. So I'm going to give it within quotes a doctor, cause if you look at the file names, they're all they all have the doctor than the extension, so this will split it by dot. So let's go ahead and test this out. I'm going to say, puts file extension and I'm going to read it in So RV zero. All right, let's save that. Let's run it, ruby file operator dot r B. And I'm going to say my sure doc text. There you go. More sure text. It's it's separated it out by extension, right? So let's now run it for the files that we have a sample that CSB there it ISS sample CIA's V Great. Now you may be wondering why it's in separate lines. Remember, Split gives you an array off the different elements that it split the argument into so basically split it into sample and see Israel's The Two elements. And since I'm using puts its displaying it in two separate lines. If I put print, there's the ray sample and then see as we see how it's two separate elements in an array. All right, moving on. So the next method I'm going to need is I'm going to need reader class and here I'm going to say extension equals file extension right there and then r V zero. All right, so I'm calling this file extension with RV from this reader class. Now, what I want to do is I want to return. See, as we reader if extension equals C as we meaning if this is equal CSB that I want to return CSB reader return yamma reader If e x t equals, why am l return? Jason reader, if e x t equals Jason And if none of the above simply return file reader Okay. And again, I can write this out as well. But, Ruby, if you remember, the last line gets returned anyway. If I don't have any other returns, any of these returns will stop execution off what's below it within the method. So let's run it. So I'm gonna say, puts breeder class. See that? Let's run it with text file. Okay. There you go. It says father reader, it returned the class. Now let's run it with sample, see as we Okay, That was a problem. Didn't work. Why didn't it work? Let's run it again, Ruby. Vaal reader dot r b and I'm going to say sample dot C s v and then run it Oh, what? So you have that made a few mistakes there. I'm going to say file operator file operator on R B samples CSP. Why's it returning? Follow reader. Because this is failing. Now, why is this failing? E x t? Oh, whoops. E x t is not see SV. This is always going to fail because I'm returning file Name got split. I'm returning the array. I need the last element of the array over here, so dot last in my file extension method. Let's try it again. Example CSP There you go. See his V reader. Now let's see. Jason, Jason Reader. Perfect. So this part is working. Now let's update this puts here. Now that we know it's working, we want to work with an object of this class. Right? So we're going to say reader class dot knew and I'm gonna say r V zero within parentheses, and then I want to run the read method, so let's try it out. We're gonna run it there. Okay? Reading file with Jason. File Reader named John Age 20. Marital status. Single part effect. Notice what happened there. Not only was I able to read in that he was a Jace on file, but I was able to now run it automatically read that in, create a new object and then printed out are right. This is working. Great. No. Notice how we have RV zero we don't want to use. We're using this in two places right now. Whenever we have something like this and this is not going to change, what we can do is we can use a constant so above here above this file extension, I'm going to define a constant. I'm gonna say file name equals R V zero. Okay. And then wherever I have r v zero, I can simply change it with the file name Constant while name there and then file name here . All right, let's run and make sure it works, okay? Still working now? Why did we do this? Why did we replace file name here with a constant instead of leaving it an RV zero? Well, this is because this is an external detail that's independent of the function off this application. Right? So if we choose in the future, how to get the file name instead off using RV zero. If we choose something else, then We only have to update this part To show where we're getting this from instead of going into the program and changing old RV zeros, Weaken, Just update where we're getting it here. And then the program will work independent of that. That's why this is an external part of the application, not not dependent on natural function off this. Okay, so I hope you enjoyed that. Now. The next step is we've been reading in and displaying contents of filing, doing all these fun things with identifying how to read extensions and so on and so forth. But we haven't done anything to the files is yet. Right now. What do you want to do? Something like what if we want to actually find the number of occurrences of award in a file? Let's explore that next in the next video. See you there. 28. Project - Word counter: Hello. And welcome back in this video, we're going to switch our focus a little bit towards analyzing files, no longer just reading the content of files and displaying it, but analyzing it. So we're going to start off by counting to specific occurrence off a word that we supply when we run the program in Ah, file. So here is my sampled up text file that we're gonna work with. I did a little bit, So go ahead and create a sampled up text file and fill it in with some random text so we can run our program on it. Okay. And once you've done that, create a new file. So right here, I'm going to create a new file and I'll call it word counter dot r b. And how is this going to work? It's going to be something like this when I run it. I want to say Ruby Word countered on R B, and then I'll say, sampled up text, which is the name of the file. And then I'll give it the word that I want counted to see the currency's off mature in that file. Okay, that's how I would run it Of course, if I write it now, this is not gonna work because I don't have anything in word counter. So we're gonna work with arms again. So first thing I'm going to do is I'm going to take in file name as our zero, so I'll use a concept like I did before. File name equals R G V. And then zero. Okay, Another constant in the program would be word to count. And now the RV one would be the second argument I supply when running the program. So all the work we're gonna do here will be in a method called count words. And then we can just run count words, right? So let's go ahead and define it. Count words. All right? And then we're gonna run counter words. So let's save this. Let's run it, Ruby word counter dot R B and then file name sampled text and the word I want to count his file. Nothing happens. Great. But at least the program is working. So step one is reading in the contents of the file. So we're gonna assign a variable contents equal filed out, read, and it will be filing. So it's going to read in following now. How do we know it's reading it in? Let's output the contents to make sure this works. So let's run it again with the same thing. There you go. It's reading it in. Perfect. Alright, some get rid of this. Okay? Now what do we want to do? We want to first. There we go. Gonna write down what we need to dio remove punctuation. We don't want to count that and new lines and convert all string to lower case. All right, That way we can count. So there are three things we've done here. Let's do that first and I'm gonna use rejects here. So contents, equal contents dot g sub contents that g sub and what does g subdue Jesu basically replaces the first argument with the second argument that we give. Okay, so the first argument is going to be all the punctuation and how are we going to do that? Are going to account for all the punctuation. We're going to do that using rejects. And if you want to learn more about rejects, you can type in Ruby Red Jacks and you can also test it out in Rue Beeler. Okay, this is Ah, testing. There's a testing interface for regular expressions that you write in Ruby. So back here, I'm going to give you the rejects for punctuation. We're gonna do a slash, and then it's not important exactly to know how this works, but it would be good to know for future, so I would definitely recommend looking operate jacks here. All right, So up arrow A to Z 0-9 Backslash. Yes, close square bracket forward slash I comma, and we're going to replace it with empty string. Okay. And then the next thing we're gonna do again is we're gonna take contents, and then we're gonna do contents dot g sub again, and then we're gonna take new lines. So forward slash backslash and for a new line, forward slash comma. And then we're going to replace it with empty string again. And then the third thing we're gonna convert to lower case so contents dot down case bang. So it takes effect. OK, so let's try it out. Let's puts contents now and see what happened. Say that There you go and got rid of the new line. Everything is lower. Case and there's no punctuation either. Known as the gaps. Those are the removal of the punctuation. Okay, so that's good. So now performed these three operations. Now I need to count the word in question. So we're going to do words equal contents dot split, and we're going to split it by space. Okay, so I m d string. So what this will do is it will give me an array. Words will be an array off, all of the contents neatly packed in elements. Each one is an element. Now we can simply use the count method. We can say count equals words, which is our array, doc Count, which is a method we can use an array, and then we're going to count word underscore to underscore Count Doc down case. So, in case I don't have just down case, I want to make sure that the word is still counted, regardless of case. Okay. And then I'll say something like this puts the number off times. Word to count appears in while lame is count. All right, actually, let me put single quote here, so it looks better for display. Okay? And that should work. So let's try it out. I want to count the number of times three was present. So sample text and then I'll say three. The number of times three appears in sample. Doc text is three. Perfect. How about the the number of times the appears and sample Doc text was one. Okay, How about my Sure? My sure does not appear in that file. So it's zero perfect. So there it is. There's our ward counter, and it seems to be working perfectly. So what I would recommend at this time, if you have some time, is to test out regular. Okay, put in the regular expression, See the expression between the two slashes, the two forward slashes. So let's take this. Okay, let's take this part right here between the two slashes. Come and see. Put this in here. And then I and then let's say hello. No matches, comma. See, it captures the comma because that's punctuation. All right, that's how you test this out. How about let's match this new line. Okay, So just slash n when I get rid of this and over here, I'm just going to say back slash n and then no matches soon as I do. There you go. Now it matches my see. It matches again twice. See what's happening. It's matching the regular expression with what? I'm entering in. Right? So that's how we basically get rid off all the punctuation, Replace it with empty string, get rid of all the new lines, replace it with empty string, and then we down, case it. And now we can do our comparison. Okay, so there's a little bit of analysis, count words, which is great. Play around with it, you know, do something new that you would like with it. And in the next video, we're going to switch this to not just count. Words will do. Well, take this a step further and will replace words. Okay, See you there. 29. Project - Word changer: Hello and welcome back in this video, we're going to work on a word changer. In the last video, we had figured out how to count to the number of occurrences of the word. This time we're going to take the number of occurrences and then change it with something else. All right, so let's try it out. We're going to create a new file, and we're going to call it word changer dot R b. And then let's pull it up from Ward Counter. We're going to copy all of this from Ward Counter. Come and see two word changer. Command V and word to count. We're going toe change this to award to change, and we also want to provide it a new word which you want to change it to. So new word equals a R g v to lips right there. And then we're going to change this to count change words instead of count words. And then when we run, it will say change words. So Okay, so we already have the base mechanism for doing this right now. We are reading in the file over here now. We don't have to do all of this right? Because we're not changing anything else in the file. So I'm going to remove these two lines, and then I'm going to remove this lie these two lines as well. And right here, this one now contents, contents, dodgy sub. How am I going to a find the word and then replace it with something else first right off the bat. I know that the replace word is going to be new word, right? So I could just enter that here. Now, this is where I find the actual word. So there is a bit of a cheap method Aiken do here. What I'll do is content that g sub and then I'm going to say our e g e x p reg XP dot knew . And then I'm going to say within quotes, slash b close quote plus word to change, plus again within quotes, slash b and then close parentheses and then find this word and then replace it with new word. All right, that's all that's all there is to replacing it. But now we have content and we have to put the contents back on the file. So we're gonna do filed out open And then the file name we want is file name. So that's the file that we're reading from that we're gonna reopen, and then we're going to, right? We're going to give it right privileges to our program, which you designate with a W over here and then we want to write it down, line by line. So do F and then f And this is the shovel operator which we discussed already. Shovel in the contents, All right. And believe it or not, that is it. See how easy it waas to do? So, what is this, Reg XP? Reg XP is a regular expression object. And if we don't use this Reg XP with the B, which essentially you can think of as boundaries, it will find words with, in other words, and then change the occurrence of that. For example, let's say f a T h e r. Okay, And I'm trying to replace the word th e. It will find tht with an F a t h e r and then replace that with whatever it is that we're trying to replace. So we don't want that. We just want full occurrences off th e right That's why we we put in the bees so that it only finds the full words to change, and this is going to accomplish that. All right, so let's try it up. Save that again. If you want to test this out, put in expressions in Rue Bealer and then test it out. All right, back here. Let's run it. Let's see which is a word that we want to change. Let me pull up sample text. Um, let's replace three with five. OK, so we're going to run. Let me clear this up. Ruby board changer dot R b and then file name, sample, doc text. And I want to find three, and I want to replace it with five. All right, so now let's look at sample doc text. Actually, this was already open. So after reopen it where? A sample. That text. There you go. Now, what happened here today was such a beautiful day. It was nice and sunny. Actually, that was yesterday, blah blah right here. What an interesting mix of five days. Right. So that got replaced by five. But see, this three did not get replaced and homework assignment. Why did this three not get replaced. Think about it, and you will take you all of five seconds to figure that out. And then back here. Total. The other interesting thing was it was only two days. Not really five. So the trees God replaced with five. Great. And this one is up to you to figure out why this one didn't get changed. Okay, back here. So there you go. Forward changer is working. And that's all I wanted to cover in this video. I'll see you in the next one. 30. JSON project - read and write setup: Hello and welcome back in this video, we're going to build a project where we when we run the project, it's going to read in permissions from Jason File. Then it's going to create users that we provide. And then it will create 1/3 file where it takes the user and assigns the permissions it red and from the Jason file and basically have users with permissions. Okay, now, this project created some problems in my other course because there was not enough explanations leading up to it. But by this point, due to the amount of all P and working with different types of files that we've covered in this course, they should be fairly easy for you. But we're going to cover it nonetheless, because it's a very good project to kind off mix in a couple of things that we've learned so far. So just to show you over here here, the permissions template, it's in Jason format. Here's my user class, which we're gonna build. And here is the runner. And when we run this anytime we run the runner, it adds a row to this file. So here is the file here the two roads so far to users. So let me add a different user. I'm going toe ad mature at example dot com when then mature. So what will happen is if I run this ruby Ragnar dot r b. Okay, it adds muss. Sure name and then email mature at example dot com and its signs mature with these permissions. Read, write and write. So let's see that took place right here. Users. Jason, There you go. Now there is 1/3 row. A short example dot com Name is mature and the permissions right here. Okay, so that's what we're going to build. So let's get started. First things first. This was obviously in a sample preview folder I created for you. Going to create a new folder is not gonna be under Opie Falls under Ruby projects. Create a new folder and call it Jason. Underscore Project. Okay. And then within it first, right. Click new file. Enter in user permissions template dot Jason. Well, we want to do here is we want to assign the permissions that are going to be read in for users by our file. I'm simply going to copy paste this. There you go. so far. Accounts you have read access policies you have write access users. You have write access. Okay, whoever is was running this program. Save that. There's gonna be read in. So there's nothing important going on or nothing that we need to know over here. The second part is we're going to create a user dot r b and this is gonna be our class. So is where will do most of our work. First of all, as you remember from working with Jason file before we need to require on within quotes, Jason. Okay. And then class, user and OK, perfect. So what will the user class do? The user class will read it. Permissions from user permissions. Template creates a new file or an existing file with the user data and the permissions data , and that's it. Okay, so those are two things is going to do. So let's build us by first printing out the contents of the Jason file. Okay, so we're gonna have a method we're gonna say, read permissions. Okay. And if you remember from working with the files before, we're gonna use a variable contents equal file docked, read, and then the name of the file. So the name of the file here is user permissions. Template, not Jason. So within quotes, user underscore permissions. Underscore template dot Jason. And then we want to print it out so that we know that it's working. So puts contents. All right, say that. Let's run this. Make sure you're in the right folder, which I am not. So I'm gonna cd out of this. Let me clear this up. So, from Ruby Projects, we're going to see the Into Jason project. Okay. From here, I'm gonna type in ruby user dot r B. Okay. Didn't work. Why didn't it work? Because I didn't call this method anywhere, so I'm gonna have to call it read permissions. Say that. Run it again. Undefined local variable or method read. Permit permissions for main object. Why's that? Because this this read permissions is within my class user and I don't have an object of class user, so I have to initiate that user equals user doc new and then right here, user dot reid permissions. Let's try it out. There you go. Accounts read policies. Read users. Right. So now we can read in the contents off our Jason file. Perfect. Now moving on. The one thing I will point out here is that the output is a Jason strength. Right? But we need to convert this to a hash so we can use that as key value combos. All right, so how do we convert this Jason String to 1/2 format? We do that by adding in over here. Jason docked load and then filed. Art read. Okay, save that. Let's try it out. Check it out. Now, I have 1/2 format. I have key value combos that I can work with. Perfect. Now we need to have an initialized method with information that's passed in from our runner . We don't have a runner yet, but we're anticipating that we're going to have users information like name and email sent in. Right. So we need an initialize method so defined initialize, and it's going to have name and email passed in, which is what we discussed before. So at name equals name and at email equals email. So there is my initialized method this way we get to identify the user or the user object right by name and email associated with the permissions So let's try it out. Let's see what we've done so far. And let's bring out the contents to make sure that everything's working as we expect. So user equals user. DUP new Now I need a name and email so mature and then email mature at example dot com. Okay, and then we need puts user dot name puts user dot email and then user dot reid permissions . Now, over here if I don't want to Since I already have the puts contents here, I don't really need to put user Dockery permissions, so I'll just leave it like that. Let's run it and see what happens. Ruby user, not RB Undefined Method name for user. What happened? Why did why did this not work? Remember, name and email. Our within our class right here are object, and we don't have attributes successors to access name and email. So to make it work like this, we need our attributes Successor. So 80 tr access er's and then name an email. There we go. Now we have access to name and email so we can use getters like this user not name user, not email. Right. Because this remember gives you getters and centers for name and email. So let's run it again. Undefined. Method 80 tr access er's They host indexer on my M. So say that attribute access er not access. Er's perfect mature, assured example dot com accounts read policies right users. Right. All right, so that's a good amount we've covered in this video, so I'm gonna leave it here, Take a look at this and try to re created by yourself if you can. Okay. Try to get to this point. And in the next video, we're going to continue to write the rest of this program out. All right, See you there. 31. JSON project - save and complete: along. Welcome back. Picking up where we left off. So now we have information read from user and displayed and information read from the permissions and displayed. So all we have to do now is combine the user and permission data into a new file. So step one when he do output the info in combined format first. So let's do this over here. We need to initialize permissions as well. So we're going to say permissions equal from here instead, off these read permissions, we don't really need to our put it on the consul anymore. We need to save that info to, say, to output to a file. So I'm just gonna have it return contents instead of puts contents and then permissions equal read permissions. So simply the contents get assigned to permissions, and then instead, off doing all this over here at the bottom, we can simply inspect user. So user equals this and then puts user dot inspect. All right, See of that. Let's run it, ruby user dot r b. There is my user. So there it is. Name email permissions in a hash like structure. Perfect. But Ruby returns the last evaluated value from all methods, right? So we can get rid of contents here. Totally right here. We don't need to do all of this. We don't need to do that at all. And then we can remove this right here. So this method simply returns what's evaluated out of this expression and that gets assigned to permissions. So let's run it again and make sure it works. Perfect. It works. So now the last part is I need to put this information into a new file. Right? So let's create a save method for that define save. And so right now, this is a hash. Well, we need to do is we need to convert this to a Jason object. So let's do that self Jason equals and then name. We're gonna say app name E mail. We're gonna say at E mail, and then permissions we're going to say at permissions, and then we're going to say, doctor, to underscore Jason So it converts it to a Jason object. And then I wanna I wanna output this first to test it out. So puts self dot Jason. And over here, I have to call the method, So I'm gonna say user dot save, So I just want to see what it looks like. So you will get to see this format and the next four months. So let's run it. There you go. There's the earlier format, and then here is the Jason Foreman. Perfect. So we took the initial Jason format, converted it to, ah, hash like structure that we're gonna work with. And then now we're converting back to Jason format. Okay, Now, let's add information here to output this to a file instead of just printing it to the screen. So over here, we're going to say, I'm gonna just delete this here. We're going to open a file, so file not open, and we're gonna call it users Dr Jason, and then we'll give it permission. A again homework assignment. Look up. What the permission a does for file Do if and then f dot puts self star Jason. Because we just want to output this to the file. That's it. And save that. Let's run it. There it is. It printed out from user inspect by user. Save didn't print anything out because you got to tow the puts. But it should have created a file. So let's look at our project right here. Jason Project. I'm going to refresh file tree There it iss There is users that Jason and didn't show up. I have to refresh file tree if I open it. There you go. Oh, sure. Sure example dot com permissions. Perfect. Now we have the user class, and as always, we don't want to have this execution code in here. So I'm gonna commend X this. Save that. Let's create a new file called Runner. So I'm going to create a new file runner dot R b First things first. We're gonna have to require relative user. So require relative user, then peace. The information user dot knew. Let's change my sure to something else. Let's change it to John and then John at example dot com I don't need the inspect here. I can simply save User, see if that so. Now let's run runner dot R B ruby runner dot r b. Let's pull off. Users are Jason. There you go. There's a second line perfect. So our project is now complete. It's working exactly as we expect, and hopefully this time you had a much easier time understanding this project and how old a different parts off it worked. Okay, so I think we are now placed really well to start our final project, which will be a little more complicated than this. OK? And it will deal with reading stock information from financial websites and then creating files with the stock prices and then running. Some announces on it. So we'll start that in the next video. See you there. 32. Preview of final project: Hello and welcome back. Over the next two videos, we're going to build our final Ruby project for the Ruby section, and we're gonna build a stock quote analyzer, okay? And we're gonna work with financial data stock data and perform some kind of analysis on it . So one of the first things we're gonna do is we're gonna build a program that when we run it, it's going to create CIA's V files with different stocks. Okay, And what the price of the stock is so just as you So I ran it. I created three CS V files for Apple, Google and Microsoft cause these other three Iran And for there you go. So it goes out, gets financial market data and then create CIA's V files with the date and price of the stock. So that will be the first thing that we do. Then the second thing we dio is going to be If we run it, it's going to give us the price and the date for the past one year for that stock that we run it for. Okay. And not only that is going to add it to this file. It we're gonna have historical price for the entire last year for the stock that we run it for. And, of course, we can run it for multiple stocks. Okay, so it's going to go out there to the financial markets, grabbed his data and then give this to you in a spreadsheet. Okay, Stobbe, the second thing we do, the third thing we're gonna do is we're going to run analysis on this. Based on the information over here, we want to isolate the price and perform any kind of analysis. So then all says that I'm gonna show you how to do is find the average price, but from these prices. Okay, so if I run it, if I run Ruby stock Quote analyzer done, R B, which is 1/3 1 It gives me the average price for the stock for the entire year. And you're gonna learn how to do all of this so you can perform any kinds analysis. You want five years worth of data. You can do that. You want to find the men, Max, You want to find average or anything else? Any kind of allows us. You can do that and we're gonna accomplish this by building multiple classes. We're gonna have a historical stock quote class. We're gonna have a stock quote class, and we're gonna have a stock quote analyzer, class. And, of course, as usual, we're gonna have a runner that we used to run all these. Okay, so I hope you're ready and excited, and we'll get started in the next video. See you there. 33. Check documentation - stock price analyzer 1: alone. Welcome back. Now we're going to work on our final Ruby project, and we want to make the focus of this project the analysis of riel financial data that is grabbed from the stock market. Well, we want to build a program that can read and data about a stock, grab this information from the markets and then create a new file, paste all of this data to it, and then run some some type of analysis on this. Okay. Basically simulating a riel life data analysis program. So first, let's go ahead and create a folder. I've already created this in your Ruby Projects folder. Go ahead and create a finance underscore project folder that navigate over to it. Okay. And then within it, create a new file and we're going to call this stock. Underscore quote dot R b. Okay, there we go. So the first thing we're going to do is we're going to get the price, and we need a ruby gem for this. So get Yahoo Finance gem to get stock prices. All right, we're gonna get this gem. And what this gem does is it uses an A P I to connect to the financial data site in this case Yahoo Finance. And then we can do whatever we want with that price. So if you want to look this up, you can go to Google and type in Yahoo fine ans Jim. And then it will bring you to the get help page for it. And then you can read up on this as much as you want. OK? All right. It gives you a whole bunch of fields that you that come with it that you can use. So first, we're going to install this gem. So to install a gem, weaken, do this gem, install Yahoo Dash Finance. What this will do is it will go to ruby gems dot org's and then grab the gem and then install it for us. So there it is. It's installed. So let's give it a shot. Let's try it out. So in our Stockport RB, first thing we're going to do is we're going to require Yahoo dash finance within quotes and then we want to say client equals Yahoo Finance Colin Colin, client dot knew Where am I getting this from? I'm getting this from the Yahoo Finance get help Page all right. This is the way they say to basically get information about a stock. So let's build a very basic mechanism to get price for a stock. So there you go, and then we want to foot client dot quotes. And this is the format. Open parentheses, open square bracket. And then within it, you enter in the symbol for which you want the price. So I'm gonna put an apple within quotes. Close square, bracket, comma, open, square bracket again. I want to the opening price. So open, close, square bracket, close parentheses. And then we want to wrap this in a class and a method. So the class I'm simply gonna call stock quote so class stock quote and then put all this in there. And let's name the method something. Let's define itself dark price by symbol. Okay, define self Doc. Price by symbol. Now, what is this self over here? Self means this is a classmethod. All right. That means I don't need an object to use this method. You see how we were using objects and all our other programs when we created an instance of a class for this, we can simply refer to the stock quote plus and then prize by symbol, and it will work without having an object created off this class. All right, so there you go open and then end this method. So let's run it. Stock equals stock quote dot price by symbol That's run it Ruby stock. Underscore quote dot r b. There you go. There's the opening price. 109.45 So it gave me the price off Apple stock. Okay, the opening price for today. If we check it out, let's go to finance yahoo dot com. Let's look for Apple looking open 109.45 right there and right here, 109.45 Perfect. So it's working. Let's try another one. Let's try Google. Save that. Let's run it 738. That's the opening price. So let's try Google. Look up. Opening price 738 Perfect. So are price getting mechanism is working, so we have a program where we can get stock prices. This is great. Now this is pretty bad, right? Cause we're hard coating in the symbol for the stock that you know this is going to run for . That's never That's never a good idea. We always wanted to pass it in. Right. So this should be independent off the symbol of the particulate stock. So let's change this and make this symbol. Okay? Now, in order for this to work with symbol, we need to pass in symbol. So we're gonna pass in symbol. So we're gonna accept symbol as argument for this method and then run this on that symbol. But in order for that to work, we have to pass in the symbol for the stock that we want. So we're here. We're gonna pass an apple. Let's see if that works this way. This method would be independent off the execution. Right here. There you go. It's working. Great. So the next step is the ability to write this to a file. We're getting the price. Now we need to create a file and write this to a file And will you see as v format comma, separated, fall, okay and see SV and Excel are pretty much widely used formats for data analysis in the financial world. That's why I'm choosing CSP. And what are the permissions we're gonna be using to write to the CIA's V file. Let's go here and go to this link, and I'll have this in the next text directions. If you go here. This is documentation on input output for a ruby. Okay. And basically, it shows what the different permissions are. What you get with w what you get with our what you get with a what you get with a plus. And it depends at the end of the file. If it already exists, otherwise it creates a new file. All right, so back here, let's write another method. We have the price by symbol so we can get the price. Now, let's right to define self docked, right? Underscore price by symbol. And this is going to accept symbol and price. All right. And what do we want in this method? We want to first have a file name. So we're gonna say file name equals and we're gonna pass in the file name. So within quotes, pound open, curly brace. We want to say symbol, which were passing in close curly brace dot C s V. Right. So that will be the file name. Whatever the symbolist. So if we're passing an apple. We want the filing to be apple dot CSB If we're passing in Google, we want the filing to be goog dot CSP. OK, so whatever the file name is dot CS v and then what do we want to do? We want to do file dot open and then file name. Since we have the file and that we're creating here so filed in and then as we discussed, we're going to give it a permissions and then close parentheses do file. And what do we want to input in here? We want to input file shovel in within quotes pound Today is date close curly brace, comma and the price. So pound open curly brace price close, curly brace and then we want a new line. So we're gonna say slash backslash end close quote. And that is what we want to enter every time this method is run. All right, so let's look at this method again. We're writing price by symbol and we're passing in. Symbol and Price were generating the file name based on the symbol that were passing in. And then we're giving it a permissions, and then we're shoveling in the date and the price and a new line so that the next time this runs, it starts in the new line and keeps going. Okay, so that is our