Ruby Beginner Series - The Basic Types | Scott Taylor | Skillshare

Ruby Beginner Series - The Basic Types

Scott Taylor

Ruby Beginner Series - The Basic Types

Scott Taylor

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
15 Lessons (1h 31m)
    • 1. Ruby Intro

      2:29
    • 2. Numbers

      3:37
    • 3. Strings

      2:11
    • 4. Symbols

      2:15
    • 5. Variables

      3:48
    • 6. Arrays

      5:42
    • 7. Set

      3:54
    • 8. Hash

      6:26
    • 9. Comments

      3:03
    • 10. Iteration with #each and for

      11:07
    • 11. Conditional statements

      10:44
    • 12. Conditonals part 2 - and and or

      9:46
    • 13. Ranges and upto

      8:19
    • 14. fizz buzz challenge

      6:08
    • 15. fizz buzz solution

      11:32
  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

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.

118

Students

2

Projects

About This Class

In this second part of the Ruby Beginners Series, we’ll look at the basic types: Strings, Numbers, Symbols, etc.

We’ll also take a look at the most common collection types: Arrays, Hashes, and Sets

Finally, we’ll conclude by showing common methods on these classes and how to iterate the collections types.

Meet Your Teacher

Teacher Profile Image

Scott Taylor

Teacher

Class Ratings

Expectations Met?
  • Exceeded!
    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.

Your creative journey starts here.

  • Unlimited access to every class
  • Supportive online creative community
  • Learn offline with Skillshare’s app

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.

phone

Transcripts

1. Ruby Intro: Okay, so why Ruby Ruby is one of the most flexible and powerful languages out there. It's also great as, ah, first programming language. There's a lot of consistency in it. So what you can do to one thing you can do to everything, and that makes it a great first programming language. It's also great as an advanced language. As you grow as a programmer, it has more and more advanced features that you can use and manipulate. It runs some of the world's largest websites through ruby on rails. Rails is just a library that uses a lot of the power of ruby. In fact, that's why rails was built on ruby. And if you look at copies of rails in other languages like PHP, they just they don't make any sense because those languages don't have the same flexibility that Ruby does. So that's a lot of the power of rails and also because it's so flexible. It's a great language for prototyping. It's really easy toe. Have an idea and just sit down and write it. Uh, you don't have a lot of, ah, sort of strange syntax, things or special conditions. You have to worry about because of different types or so on. Now you might be attracted to Ruby just because you want to learn rails. And if you don't understand ruby, at least foundational ruby rails will be totally mysterious to you. But I think even having an advanced understanding of Ruby Ah, we'll give you a really solid understanding and mental model of what rails is doing and why Rails is doing it. So there are some goals for this course, and there's only two. At the end of this program, you should be able to read simple ruby programs and right simple ruby programs. So it's a simple is that you just want some foundational knowledge of ruby so that you're decently proficient at Ruby. 2. Numbers: All right, so now we're gonna talk about Just serve. The basic ruby types on this stuff will be pretty obvious to you, but there are some gotchas so which is good started without. So I'm gonna open up Irbe and the first thing we'll talk about, It's just number. So that the number one number two 100 1000 or 10,000? No. One gotcha is that if you say 1/2, it doesn't say 0.5. But if you say for instance, four over to it does say to 5/2 is also to. So the reason behind this is that these are just insurers. They're not what's called the footing point number. They don't have any sort of sense of a decimal. You can say dot class, which will tell you the type of something. So, um so these are all fixed them. So if he is a really big number, it seems a big meme. And these air just internal representations what Ruby's doing under the hood. It's not really that important right now, but just know that you can get that type of something anytime you want. So now how do you solve this? one over to issue. There's a couple ways. So one is that you actually change it into a foot. So now you see, you get the right answer. You chewed auto that class and get a felt. What? But if you said to doubt class, you get a fix them. So one option is you just, you know, make one of them afloat. You could make both of them folks, too. That would work. But as long as one of them and it doesn't matter which one, you'll get the right thing. By the way, I noticed the other day that python has the same issue. So Python works the same way as Ruby. In this regard, another way you can actually solve this is to call to F, which transforms a fix them to foot. So you said class here, get a foot. So that's pretty much it for numbers. Um, we'll use them a little later, but most math and stuff is pretty obvious. Um, one little thing is to say exponents. So for instance, so too. So when you say power, he actually do two stars are to time. It looks like times twice on one of Ruby's nice things is that you can do very intense math if you need to. So, um, another thing they know is that this won't work. Um, so you could just say 1000 Or Ruby gives you a little extra bonus and tax with a allow you to put underscores so you could represent a 1,000,000 like this. But just what you know, this also works, right? This doesn't mean anything, really to us, but it might in your context, but it doesn't normal decimal numbers, so just f Y I. So that's pretty much it for numbers. Let's move on to strengths. 3. Strings: All right, So now let's talk about strength. So, strings and Ruby, you very seen it, for instance, High or how? Well, And there, uh, serve marked off by these double quote marks. And you can also combine him. And with that Plus, so it's one way strings work. Um, slavery seem this. Hi. New on. So here it's actually just echoing back what we have, but for some prints, so that's a string. Now you can also represent ruby strings with single quotes. But you should know that these control characters don't get escaped. So if you ever want a literal control character, you can use the single croaks. You could also do this slash slash and which also gives you a literal control character. So that's the basics of strings. Now there's also interpretation, which will talk about is just a fancy word. Let's just talk about it now. So let's just say and in variable right and variable like name, so I could create a string because high name. And so what I did was substitute in this variable, so I could I could have also said, hi space plus name that works too often. This is just a little easier than using. Plus, in the past, they used to actually be more efficient to do this. These days I don't think it matters, But often doing stuff like this is a little more readable. Or use a pound Norges just so it's a little more obvious pound and then these symbols and your variable in there. 4. Symbols: right. So now we're gonna talk about symbols and symbols are something that you don't see in a whole lot of other programming languages, but they're kind of like strings. Um, there's there's sort of memory efficient strings, and you'll see him all the time and rails. And here's what a symbol normally looks like. So it's just a colon and then the name. Now, if you had a space, you really have to do this, which is also symbol. You can cast a string to a symbol, so two sim and it'll turn into a symbol can also cast a string my symbol into a string to us so you can go back before and by the way these are called Methods or functions will go into this later on. But, um, we're a saw it with class. We've been called my symbol that class, so it's a symbol on my string class string, but that's the basics of symbols. So what they're really doing. And here's a good example. So what we did here was we said, run this 10 times, and each time we go through the swoop, create a new string called Hello World. Now, if we did this. If we did it with a string, we actually allocate 10 strings. So imagine if this was like this. Then we allocate 10 million strings are a program might get really slow for doing symbols, though. Um, we'd only allocate this once. And so that's the main thing about symbols. Truthfully, usually you don't care. Usually you can use strings and symbols. Interchange way, um, rails does a ton was symbols. So, for instance, has many comments. This would be a typical rails. Statement leads union use symbols by truth way, you can usually use strings and Bill just cast it internally. But that's the basics of symbols. 5. Variables: all right. The next part is variables and variables are a little bit different than they are in, say, math context. So you re seeing this. You can assign variables to anything in Ruby, and if you just type the variable name, it'll echo back. The value in Irbe Close is a print exe, for instance. Now variables are changing. So and that's what I meant by their different. And they are in the math contact. So let's say X is 10 now. We can say X is 100 so obviously access and stayed constant. So variables are variable. That'll change. So another thing is that in ruby variables, it doesn't matter what type they are. So in other languages you might have a string variable or a sane job. You might have string X or, um, you know interest. Sure, why? And Ruby, you don't ever say a type, so you can just think about it being totally type Wes and um, and presumably Ruby knows how to cast stuff that's of different types. So, for instance, let's say, uh, X equals to my string across 10 right? So sometimes you got type errors. Um, could also use interpolation to, but so it doesn't care. Um, what the types are so you don't have to worry about casting stuff. And usually, I mean, there are cases of this would be, Let's say to us, it actually turned this into a 10 uh, strengthen. But, um, so that's the basics of variables. Now, there are different types of variables, like instance, variables on class variables. They're also Constance, but we'll just stick with the basic variable. Now, variables can be named anything you want, anything you want. But a name like this is actually bad practice. Generally, the Sir convention in the Ruby world is that you have all the variables lower case. You separate the words with underscores. So like this. Not like this, which is Camel case. No, this so loud. But I wouldn't recommend it just because it's sort of bucks trend. The trend. Everything is upper case. So uppercase is generally reserved for classes or quote unquote constants, which aren't really constant. Ruby. Um, so generally you don't want that, and you've probably seen that before 10 dot class or string, which is a class. He asked, What classes? Classes as a quest. You see, these are all uppercase. They start with uppercase, so that's generally how you'll name constants or classes. They start with uppercase, so normal variables like, uh, my first name. Do you really want, like this and what's called Snake Case? Because it kind of looks like a stink. 6. Arrays: So now we're gonna talk about collections, questions or just, you know, groups of things. And we'll talk about a raise. Cash is, and we can even throw in a bonus of sets. So the first thing isn't Ray. So array is just a question of items really under the hood. It's a link twists. If you don't know what that is, don't worry about it. You can just think of it being a question of things. So of this list here, and you can reference the items in here with the index index is always started. Zero just like a common programming thing. Now we go way outside of the bounds, so you just get no, it won't grow up or die or anything like that can also negative and backs so you can see the last element here is for And I said, What's the one before zero or negative one I got for? So it's our handy that you can do that. So one common thing you might want to dio with an array. I may have my name, you can join, and then you get a string out. So if you're ever curious about it, just killing. All right, Join. You can see this is Honore on This says, Hey, there's normally separator, but you can see these examples So this is pretty much right in I'm so indexing and joining is are some of the most common things you can dio. You can also add to us. Yes, our cat really also works. No, Uh, that's right. So you can could cat a different list. So if you want to join two different race together, you could go and do that. It's very much it for a raise. For now, you can look at the docks and you know, this is one of the most and they must use, you know, classes and ruby. You'll be using this all the time. So there's lots of convenient things like any or select one where it's, you know, greater than three. For instance. There's lots of stuff you can do with the rays. Um, one thing to know about a raise is that they're not unique, so you could have this, but there's unique which returns in you, right? So if you work, it acts hasn't changed. You. This has changed. But, um, you could also do this so extra unique and reassigned tax you see now it's changed. So usually these. This is what's called ah bang method exclamation point. Um, and it means that it's changing its internal state as opposed to one that doesn't, for instance, this unique without the exclamation point and does not change the internal state. So just show this again you call unique. It will return you a new array, but the original one isn't changed. But if he called unique bang, you would actually change the internal state. So it's just a ruby convention. Another Ruby convention is that that functions with question mark that end with a question mark. Return you true, false And I guess I haven't talked about that either. Is there is true and false also Neil, which just means empty. So one double eagle to true. It's false, wonderful, equal to one. It's true. One is less than two. True to is less than No. Two is greater than three. So, yeah, so these question mark methods like any you might ask, does we don't know what's in here. Is it empty? No. Does it have any? It does. So those air to conventions around around functions that if you have a question, market returns you a Boolean true, false. And if it's a bang method, it will actually change the internal state of the object. 7. Set: I want to take second. Just talk about sets. So no, such are different than a raise and relieved because you can't have a new set with duplicated items. So and Ruby, you actually need to require some. So if you do this set, we'll say, Oh, I don't know what set is. So you have to require it. It's actually in the standard library. So if you say all right, set reveals, find out. So you noticed they require set. So there's some things that come bundled with Rubies from libraries that come bundled with Ruby. There aren't part of the the main serve court language and set is an example of it, so you'll have sort of choir set. Now, some environments like rails have Ray Carter this for you, so you don't need to do this. And if you require something a second time, it'll say false meaning that's already been required. So no, we have set. Anyway, you can see that. We say set dot knew 123 We actually passing Ray in. Um what kind of it just looks like a set like, uh, like an array. But if you call Chueh, you get an array back, but you'll notice that do this. We don't get this duplicated thing. So we showed this in the last one. We did something like this when we called unique on it. So I mean, there's really no reason to use sets unless you're continually adding stuff and you always want to make sure it's unique. Often you can just add stuff, you know, they could say axes want you, and that may be 100 times. Teoh X is something. So now X is 12 and then 107 so you could just call Unique at the end of it. So this might be a little more efficient if you're adding which stuff and you always want to make sure it's, ah, its unique there might be a little more exact because if you forgot to call unique here, you know, it wouldn't be unique. But so just ah kind of thing on sets. You can also add sets. So I think this works. So you see, it gives you in use at or you can subtract sets. So is this the saying 12 and three and then subtract everything from this set? Um and ultimately good cast a chin ray want you. You can also subtract a raise to So So basically returns to the same thing so sudden a rare , really, really similar generally mess. We will just use the rays and call unique if that's the behavior you need. As always, you know you can or ice up. And these are all the methods. We'll talk about the difference between class and instance methods and a little bit. But I just know that when I called set dot on you in the actual class, that was a classmethod, so you can see it was lesson here. But when I called, for instance, minus, that was an instance method. So we'll talk about the distinction a little later, but I just wanted to say something about sets toe, differentiate them from a raise. 8. Hash: all right, so our final sort of basic type is the hash In other I oranges. This might be called a dictionary and python. A lot of people just say addict, because that's what you do in private phone. But in Ruby, we caught a hash. So one is a hash. It was just a key value store. So might say first name Scott, now often in Rails world. These hashes, though, have the keys as symbols might look like this. And then any time you want to know or you want to look something up, you can use the bracket method. So So, hash Last name, this tour, first name and Scott. Now there's a new syntax that started in 20 where you can make it look like this. And this is because the whole symbol to whatever string or whatever was just so popular. Um, this also really looks like Jason, which is, if you're not aware of it is just a form out that's used on the Web. Kind of everywhere. Um Teoh communicate between computers. So this an example, and I'm just taking this, but so it's a little different cause we actually don't want this kind of stuff and this looks okay to me. So agency it looks very similar. Surgeon, see, hash age. It was just a way to look stuff up. Now, what's interesting about Ruby is that in a lot of other languages you need types, so you'd always need a symbol to a string in se Java or some other language. Here. You can really have hashes with mixed types. So the sun example. So we have a judge is 31 But the name is John. It comes from New York, So So yeah, that's pretty much it around hashes. Another interesting thing around hashes is that you can actually have keys that are different. So if for whatever reason you won't say in 10 maps to 100 you could do it this way. So now you can see cash. 10 laps. 200. I don't know what that means in this context, but you might have a context in which you want numbers to map two strings. And as we showed here, we can get the values. But you can also set the values so it's just acts like an array. So if you're Honore, three things you could say x zero. So that means the zero with element of really the first element is one. But you could say, Oh, now it's ah 1000. So in the same way, a hash kind of works that way. So if I said What's the name I get? John? But I could reassign Teoh Joe, for instance. That's the basic of a hash. It's just a key value store on the keys. Could be anything in the values coming anything. No. One little extra thing they know is that you can only have one key her value. So it's for instance, we have hash name right now, which is Joe. So it's not like you could have multiple names in here. And if you wanted to represent something like that, you might have an array. So an array of hashes. So, for instance, people this name Geo first name So and these Commons, the women entries. So this is an array. I'm just gonna copy this in so you can save people. Zero First name Joe, right? So that's pretty much it for Collections will talk about iterating through these and rooting through these in a minute, but yeah, That's pretty much it for the mean sort of basic questions. And you'll be using hash an array over and over and over again, so it really makes sense to get use them. And, as always, you can look up stuff with our I R. Just Google real be hash. And so here you'll see all the methods so you can do things like, you know, does it have some key? So people zero which hash? No, I mean, we can see that it has the key. First name. Haski. First name. True last name. False. So there are lots of methods here that you can work with Princeton's values. What are all the values? Not just the keys, but the values. So, yeah, there's the basic types we'll talk about entering through these in a minute, But first we're gonna talk about comments just pre simple. So let's go there 9. Comments: So before we move on to federation stuff, I just want to talk about comments. So comment, sir, like an essential part of every program. Now there's always a debate as to what you should comment when you shouldn't. You really just have to use your best judgment. But here's an example of a comment. So comments always start with pound, and they're just code that you don't run. So years. I was, uh, people. I only included Go on, Tom. I was, whatever. Whatever you're gonna right. So basically use comments just to kind of explain what your programs doing now. Usually maybe confusing parts. You want to comment generally, you don't want to comment like every sections, right? So just reading this code, it says, Okay, there's a list of people or an array of people. And there's one with first name Joe, one with first named Tom, right? So comments not you don't want to write a comment that says, Here's the list of people Joe and Tom. This is pretty obvious, right? And so let's say we had Sally and here now this comment actually realize about what's going on in this coat so you want to be careful with comments. One thing you might want to dio. Let's say we didn't include Sally here because she whatever. But just so you have to be careful in your comments now, some languages have, uh, back sash as a comment, for instance, JavaScript or C Ruby doesn't have. You just have pound for the comment. Usually you put comments on their own line. You can make multi line comments if you want. So this is all just code that doesn't run. But you can also put it here. So, for instance, you know Joe GOP, you know, or whatever you want to say. We're including Joe because there is some sort of special case. So that's pretty much it. With comments you're copying and pasting this stuff into our be you'll see that it kind of won't know what this thing is. So if you just say you it kind of weight around expecting you to finish something, so I don't just I kind of ignore this fine, and that's the same thing if you're actually running this. So we said, puts people here. They see it just printed first name Joe, first name, child. So that's that's pretty much it to comments. That's not really a complicated, just something you should be aware of. 10. Iteration with #each and for: So I'm back in. Hello, world. I'm just not Ah, Crane, you file. But you could if you wanted to you. And now we're gonna talk about looping. So what? I mean by whipping and just being iterating or going through the elements. So, um, I'm actually gonna open up my RB. So, for instance, you could, um And each this function called each is the normal way. You look so thanks for taking a copy this out here each and then you say do elements and then puts elements. For instance, So the says crane array of values 12 and three as the elements and then for each of those elements go through and we're just gonna print him out. I'm just gonna copy and paste the And so you see a permanent 12 and three each on separate lines that I could do something like this Element plus 100 1011 out to you in our three. So each is the normal way you iterated, um, and each also works for hashes. So let's say I just use this again, helping piece of actually my cousin say 30 So key value, No decrease this a little bit. So we could also put this in a variable if we wanted to, like person like. And then we could just a print value. Our puts values from dinner so you can see it. Just print. His first name is last name and his age. Um, we can also ness these things too. So we could say people which isn't right. Do you any person come in person each you key And here we'll use interpolation. So I will say the key is key and the value is the value. I'm just gonna copy and paste. So the key is first name values show he is first name. That is tough. Um, so each is the normal way. You do it now in other languages. Often what you have. I can see baseliners or C JavaScript. You'll have a four statement, even python. So you might say con numbers Don't say so. Four acts in numbers and then prints our puts X plus 100 and then you have to say end here . So just did the same thing is the, uh, each But, um, four isn't used the whole lot. And Ruby, In fact, I almost never see code like this. So you almost just always used each. So that's the same thing. Now you might ask what happens if you want the index so you can actually say eats each within next, and then this will give you the index. So 012 beer output here. Zero 12 No hashes work pretty much the same. So person it was. Copy this and last name you are so And I guess we have seen this key value. So you could just print out the key if you wanted to, or the value if you wanted to. Now one common thing and you'll see this in a lot of languages. It's more of a convention than anything else. But if you're not gonna use one of these values, you might just do this. Which is just a way of saying, you know, ignore it. Now underscore will actually become so I'm just putting an underscore. You could do underscore key. It's just sort of a convention. Say it's not going to be used. By the way a neat little irby trick IHS you can actually use underscore to reference the last value. So maybe you know he had some really crazy thing and you didn't save it in a variable. You didn't remember what I waas You could just use underscore to get it so underscored a kind of a multiple meanings. But underscore this underscore er to reference The last value only works in R B is not a role ruby thing, per se. So yeah, that's pretty much the basics of it. Oration with each use each do you elements and you have to put the end for an array or each with index. And then you get that next for hash, same thing each, except you get a key in a value and you So that's pretty much for iteration. You can use the fourth thing, but I would just recommend using each. Now there's some more complex federation things with, ah, select detect map. We're not gonna cover it, but you can look it up. I'm just our I array pound select, which means it's ah and instance method or a method on an array, not the array class, but an array itself. So these air pretty powerful methods. And if you've never seen stuff like this, that's pretty powerful because you can sort of generate without need to write these custom loops every time. So, for instance, let's say you only wanted even numbers, right? You know, normally you'd write for Why annex? You know, if X as divisible by two. This means mod. So that's just a macho. So one Montu was one Jim onto. There are three months years, 14 months to zero, so my two would show if it's even our on. So if you so if it's equal to zero, then it's even. So this might be a good example of a comment. Even Krantz Axe are just quits. Thanks. And then we have to put ends here. Oh, yeah, This should be why this is also a thing about naming your variables like I'm just using X and y here because it's, ah kind of trying to go fast, But But generally you do something like this. So when you have a confusion in your names, you'll generally have bad things happen your program and you won't know why. So so what happened here? I just do this. 1234512345 That doesn't sound very good. So That's Prince number here. Conscience thinking. See, Number zero was false. We're sorry. The value one was false. So? So got here. Number is number you are doing care is going through the numbers. We're saying, Hey, if it's divisible right, you just print out the print out the number as well. So I got to Andi for but we talked about before. This is the more sort of idiomatic way to do is with each and I'm using. If here, I think it should be clear what I'm saying. So if this number is divisible by two, then and this is a good place to put a common so if it's divisible by two right out, Right. Okay, so that's pretty much a refrigeration we're gonna talk about, if, ah, with next lecture 11. Conditional statements: Okay, so next up, we are going to talk about condition ALS so conditional. Zehr Just if statements or else statements Um, so you very seen one here, which says, if this whole statement is true, you know, print this thing out So you could also write it like this. You want to with princes, But basically, this whole thing has to equal. True. So this would be another waiter, right? This you never really do this. Double Eagle means true. Our double equal means that actually equals it's not. Assignments on assignment would be excess 10. But this would say this would evaluate to true. So, for instance, Axis 10 access. True. If excess tendon that's true. If you did it, 11 would be false. That's a double equal means. There's also not equal, so X is not equal to 11. True. So anyway, that's basically how you test If things are what you think they might be. Um, and the basic statement is say if numbers were going to use this length here, so I would do this. I'm just copy and paste. So obviously there are five things here, but you could say number is darling five so we could say something like if And I'm just gonna comin out this code, um, in Adam, you can and I think in a lot of other textures Come unit command, forward slash or the question mark. My child was comin out a Siris of lines. So you need to do this. So I don't comment out It also, if it's already commented and you select it in a long comment, so just total handy tip. So here's an example. So if numbers length is five prince ah, say the length is fine else the length is not high. Okay, so I'm just gonna come to this part out, and then I'm going to copy all this. So this assigned it this array. And then if numbers that link those evil five print dealing has five otherwise else Prince , the length is not five, and we always understatements with him. So in this case, we could have just on this so you can do it if a wound, you can do it else with that. If so, this thing isn't true. And we could do this show. It's true. The wife is not five is, though, statement. So let's say we added numbers on 100. So know this certain way If we ran this that also going isnot five another conditionally conduce Elsa. So let's say numbers is ah Sinton. So just to check this out is less than 10. So here we go. So the length is lesson 10. All right, so it evaluated this. It was false. We could check this out like this. That's false. No, no one down to here. We're in this. This one's true. And now it's not gonna run this, so that should be pre obvious. It's only going to execute one of these. So that's basically how ah, if else statement works, another thing star goes just truth e values and false e values. So obviously we did equals here. You could say not equals or less than or greater than you can also do this with strings. So name is Scott. They could say if named illegal Scott like this just you now statement. My name is what's doing interpretation. My name is not Scott. So my name is Scott. Obviously, the name has got So I said tres we re ran just this part. Just this part My name is not Scott, so you can see you can use double equal now. Um, if we evaluate this, it's true, right? Well, now it's false, but it was true. Now there are certain things that might evaluate. So maybe we didn't have names set. We just had it set to nil or an empty value. So my name is name. Or I would say, in the else clause, there is no need except it's now if I run this by the way Command K, based on a back clears the terminal. So it also works in Irbe. So set my name equal to know. And then what we're saying here, obviously name is now also reading this. So we're running into this case. There is no name set. So this name I was falsely, it wasn't actually equal if also is equal nil to really nil and false are the two false e values. But say it was so I'm gonna do this coming out this line from this sign. So now if I run this ad cause my name is Scott So it said this is truth e. It's like it has a value, basically, So anything is truth, E that's not know or false. So I could say, if one prince truth E else Prince False e you can see it turned out Treat me. That's the main thing you need to know about conditional XYZ that the structure is, if some condition and or if some condition else something else, Yeah, or if you want a nest it you can say if condition it's true. Ah, condition to, for instance, second condition. Oops. The second condition industry else. House statement. Yeah, us cause. And by the way, you can also ness these So you could have a NIF in here something you could also run multiple also shows if you wanted to, so you know, something like that. But also work. That's the basics around conditions. Theater thing is just double equals versus single equals, so double equals, actually signs and double eagles tests. So, for instance, that's false. So just be careful that you never do this. You can do it. But, um, you know, for instance, name doc length because this will always be true. Another thing to know if you come from javascript or maybe a sea based language, um, in those languages if zero is false. E But, Ruby, this isn't the case. So so just be aware of this. So I'm saying if zero put us zero so print zero with the new on. Otherwise, Prince, not Sarah. So I'm gonna run this just clear this. So if zero put on zero so zero was truth e on actually strain it out in some other languages, like JavaScript. This would be false, Ian, and actually run this cause. So no and false are the only things that will evaluate to false notice. You could also, um, for a lot of causes, the devil equals true, But usually you don't. I need to do that. Another thing you should know is that there is this method or function on every object called Neil. Question mark. So false doesn't exist, but mill does. So you can ask any anything if it's no. So what should say? You have some very bowl and you don't know what it is. You can ask if it's no. So once again, the question mark methods returning a boolean. But if it was anything else like some number you could ask of, it's No, it's not know. So and false, by the way, is also not know. You could also do this. So some people, for this some people for nil, it really doesn't make a difference which one used. So that's the basics of conditions. 12. Conditonals part 2 - and and or: Okay, So just another Ah, serve. In addition to the conditional statements video and this is really around multiple having multiple conditions to erasing this, if true from a gunshot ruby file. So we get the highlighting. So if true, you know, it's true else it's false starts to say this should wear. I don't want that. That's true. Is of course true is true. So that's a first name. This Scott last name is to our. So I would say we want to check both these. We could do this double equal. Scott. So you've seen that. But what you haven't seen is this double and person means and you could also write end in Ruby. But there are some precedents issues there. So generally we just used and so they should be true thing. And it's true. So what if we change was to Tyler Now it's false. So you see the end dead. I made sure that both were true. You could also use else are sorry nowt else. Or here perhaps So the first name Scott or the last name is Tyler, which is Taylor. And it's still true because one of these was true. Now it doesn't matter which order of these going so you could switch the order, go copying paces, so it's still true. And by the way, if he just ran this, you get true. If you're in this false, So one way, if you ever if you're just trying to test code to another way, you can do this as you can say false and whatever because you know this will never be true . So So it's false because they said it's false and some other thing which I don't care so often. If you're just trying to test code and you want to see did it go down this, like this path, you could just say false end and see what happens if it goes down this path. So it's just a little kind of quick hacky thing. I had to do so. And of course, you can also use thes and statements like else if statements if you want to, so you could put it here as well. We're not gonna do that. But another thing is, you can assign these things two variables to write. So what's a name? Matches name matches so we want. And here so you could also write this the name. After this, the name doesn't match, the name doesn't match or we could say else if name partially matches the impartially. Nash is so name harshly Mansions and Harrell using or so partially captions. All right, so that's pretty much it that's hemmed and then you have or and double equals. There's also not equals. So which just looks like So we could say first name is not equal to bomb, but it's not Bob. It's not about. And if he says it's not a gold Scott, then it doesn't print anything out. So your basic, um, ways to test her wgal not equal ampersand to combine things is they end also in Ruby documentation. You'll you often see this command like this pound or number sign, and then this era, which looks like a hash often called a hashtag it so food of our it's called a hashtag it because it kind of looks like a rocket. Um, so So the end, Uh oh, yes. So this you'll see this in really documents. Ah, lot. And it's just because Irby does, too. So that's the result of the statement. So if you just goto really dark? Um I don't know. Alright, are I agree? You'll see, Like right here. What that saying is if I sent my array equal to this and this is the result I get. So a raid on you results in an empty array. First say a raid on you three gives me an array of three elements with their own new. So that's what the little hash shocking thing ah stands for. Sometimes you see it with pound and no space, and sometimes you see it with space. Doesn't really matter. It's just a comment. So So we have the double equal of the not equal and in the or now Ruby also has and and Or and if you wanna look it up, difference between and and person. So if you want to look it up, then talk about how this works. So one thing to know, too, about thes as the evaluation. So let's say you're to call some function here and we'll talk more about functions. But let's say you're to compute something so X is equal to 100. Let's say we just had something like this, so it's their first name. It is all right. She said the first name and go to Bob Access 10 and then here were not doing double equal or doing single equal. So this means set X 200. So in this case, what do you think X is gonna be? Is it gonna be 10 or 100? Well, it turns out it's 100 because the first name was known equal Scott So ran this thing and it also ran this condition inside the are the f waas. True. So the if was true. I didn't said your 100 But let's just say we did this. No, this thing we know will be false was the first names, Bob, but it's testing it. Scott does set this. So let's make sure our exes really 10 and then monster here on this. So what you can see is that the extent change never ran this thing. So it's short circuits, no short cir circuit. So this thing turns out not to be true. It won't run this so won't modify it. And the same thing is true. Canned pierogies. And here just try it. So you see extra and change to 100 if he did or here should. So if you're really curious about Adnan, or you can just Google, what Ruby end? Or and there's, um, some stuff about precedent. So all that means is that certain things will evaluate before others. And if you're to use an in or, uh, reads a little better, but I would just suggest using that to ampersand zehr the two bars that yours 13. Ranges and upto: okay, So before we get onto our next challenge, our first challenge, really, I just want to show you a couple of other things. And this first thing is actually another built in type, which I didn't mention. It's called a range, so I would say, one of 2000 this arrangement. It's actually a range of each of these numbers, so you could say this my range, and then you could actually use each. So do number puts number. You can see it printed 1 200 And of course, you could accept this toe any number. I'm just sitting up, up and back to get each line. That's the basics of ranges. You can also cast these to raise so you can see 1 202 A is 1 200 There's also this other way to cast Um, so this will take things like No. So if you cast nil to a E means basically the same thing, there's some subtle differences. We're not gonna go into it if you're really curious. I think this also works, so there's some subtle differences. So two eyes two inch er, some subtle differences. So, for instance, this is more strict going no to I actually get zero. But this thing gives you an error. So it depends what you're trying to do. But the basics air that arranges just, like an easy way So you wouldn't have Teoh. You know, for instance, if you wanted, I was in numbers like, you know, one 200 you would actually have to sit there and type this right 567 and so on. So you could just say X is one 200 actually do this, I believe. But usually you want to surround them with Prince sees because you might run into some weird us and tax things if you don't. So so, yeah, so Prince, no, you'll see it printed 1 200 But I didn't put in the new in. So that's why it's all on on one line. Another little handy thing is sort of related, arranges, and this is actually a method or function that's on the number itself on a fix them and it's called up to. So you can do this. This says go from one up 200 and for each number, print out the number you could do anything you want here. Right? So you could say X is I would say some is zero and then some. Ah, equals to some cost number. And then you Prentice some side stratus. Okay, so if I add 1 to 2 to 3 to 4 and so on, I'll get 5050 Just that. Check this. We could dio this. I got sex ring said three plus two plus one in six. You could also do this with a range and each if you want. So I also got six. Let's say for so that should be 10 now, so it's 10. So either way, you can use one up too or a range. You could even cast this to an array. It should be the same thing. So one of two works range works. Either way, it works. One up to is just really handy. If you want Teoh, do something like a ton of times. So for instance, one thing you might want to do, And by the way, this thing where we say do number as an argument, you don't have to include this So you could just say hi or whatever you want to do here and So I printed out high 1000 times, so we didn't actually use this number here, so we could have used it. I'm gonna say puts because it's a little these air read Sisi. It said hi, Age of the times. Now, as we discussed before, you could also do this. If you aren't getting use it, you might be a little clear if you didn't know how up to worked by you could also just excluded. Doesn't matter either way works. So that's pretty much it for ranges. Of course, if you want to, you can always do this. Uh, class. You see, that's a range. And then if you wanted to, you could look up range and see here They're casting it to a. They also have links taken sailing tips. And I guess that yes, you have to call it you a, um and you can see here than methods. So this is they say, men or Max or this one looks kind of interesting. Like include right, So include seven. It's true. Include 100. False or cover looks interesting. I've never actually used this before, so it's see what it does. Presumably, it returns bullion so it does so Return trip object is between the beginning and the end of the range. So this is interesting because they're actually using characters. So they're saying Between A and Z, these are all the characters of the Alfa Alphabet agency really powerful that that all this stuff works on multiple different types. So it's not like you need a different type four numbers or strings or anything. So does this cover the I've ever seen? What do you two x The covers? E no see? Yeah. Lots of power in these ranges in the sort of type system in general. All right, that's pre manship. We're going to move on to the challenge, and I think it goes without saying, but you will learn the most by just doing. And so these challenges air Super important. Make sure you get this. If you have problems, just watch the other videos. They have the solutions to all these things, and then we'll have a video at the end, just showing you the answer so you can compare what you did. Yeah, so let's get to the challenge 14. fizz buzz challenge: All right, so this is really your first programming challenge, and this is something that's actually asked in interviews. I think it's like, Ah, not even worth asking and interviews If you're looking to do this professionally, I mean, this should be very simple stuff for you, but if you're just starting, I mean, this might be challenging, and you should definitely learn how to do this. So this is called Fizz Buzz. And if you just go up, you're clearly find solutions. But do you want to learn this stuff? And you warned by doing so this first article explains it. It's on the sea to Wiki, which, you know, if you've never seen this, this a really it's like a very old school programming wiki. This guy, this was actually the first wiki that was ever created, and it's a ton of programming stuff. Siegen search for anything on here this fine page. So, for instance, if you wanted to know about object on and said programming all right, Newby or something, you could search here. I guess that didn't work very well. Yes. You want this full, full text search. Okay. I know a lot of articles here, But anyway, the point is, I don't use this a whole lot, but it's it's really interesting and find out a lot of interesting things. So these are, like, basics about Ruby, and we'll talk about classes and stuff a little later, but okay, so onto the tests. So this is basically the essence of the program. So I'm gonna put this I'm gonna see this, as is because his buzz notes and I'm just gonna form out this a little differently. Oh, yeah. So radio program that prince the numbers from 1 200 but for multiples of three print fizz instead of the number and for multiples of five print buzz. And if the numbers are multiples about three and five, you print fizz buzz, So fizz, buzz example. Output. So here's the output that you should get one. It's the one is not a multiple of three. Two is not a multiple of three stages print out then three is a multiple of three, but it's not a multiple fire, so you print out fizz and by the way, you can put these on several lines. You don't need to put commas between number anything, so you should get. How what that looks like this to fizz four is not a multiple. Five is not a multiple. But you should get Buzz. I see. So it's farce. 123456 fizz 78 No. Nine fares. 10 is Buzz. And then when do we get our first fizz? HMAS? Yeah, 15. So just copy this, and I'm actually going to do some really magic right now. So there's a method called Split, which is kind of like the opposite of joint. So you say split on the comma and this gives unit rank. So a woman's so I want comma space. So you noticed that there's a common than a space. So no sweats? No, I have my moments, and I can say omens each do Prince puts. No. All right, so I'm just gonna copy this. This is just because I'm lazy. Laziness is a great virtue if you're a programmer. So usually a little extra work like this, um actually saves you a lot of time. So you could have started here and just done this, but I decided to actually right a tiny little program to get that output. I wanted, so All right, so, uh, go and try to write this. So the basics of this are you write some sort of loop the roofs from 1 to 100 and if it's divisible by three and five year eight fizz buzz if it's divisible just by three direct fares Duvets divisible by five. Right buzz. And otherwise you rights. That number are you print out the number. All right, That's your challenge. Go for it. 15. fizz buzz solution: Welcome back. How do you do with this challenge? We're now just gonna go through and I'm gonna live. Could this and we'll see how it goes. So first of all, I'm just gonna comment this out. Just say example Output. So these are all just comments, So I know. And I'll say this is some people like to do this, but I just put, like, kind of what, What you're thinking thing does so called this fizz buzz notes. So I'm actually just gonna inducing this stuff. So, by the way, command Z one dozen command shift Z will redo. And if you're on windows, it's just one knows or control. Okay? So I'm gonna say this as physios notes, and then just copy this at saving us. Praise buzz solution. Okay, so they're a few different ways. We know that we have to go from 1 200 So we've seen two different ways to do this. Maybe even three. So one of 200 should do it so much. Let's make sure this part works. So I mean, a copy and paste this so we know this work we could puts to make it more obvious. Okay, And then we said If the number is divisible by five and we saw this in older lecture So just to test this and the number is divisible by three, that's part of the number. So we saw that 15. Here was the 1st 1 So 15 30 45 60 75 90 So it seems right. So what's our next cause? If else if no is divisible by five and snow So what's not Do anything here. Let's see how this works that is puts So what we get Looks like we got all the numbers council say true here. Oh, so this was a little misleading. So so six divisible by five is one, but one is true fee. So because it's just not know So we have to do this, people zero All right, so that's right now. So 5 10 15 2025 This looks good too. Their last for almost last cases. Numb divisible by 30 Let's just try that one out. Well, stopping at five and so we should get 369 12. So now what's interesting here is we didn't get 15 because that was in this cause. That's good. We want print visit buzz here. So let's start back. So fizz, buzz and then five multiples of three print fares So puts his Harrell puts buzz. Okay, so no, the only thing we're missing is things that don't fall into any of these cases. We want to print out the number. So so else what's numb? And I think that's so pretty much Do it. Try it out here. Oops. Hit a accidentally. So I got error. But it's not a big deal. It's rerun. Okay, so just copy and pasted this code. So one to fizz. 45 sir, for buzz is 78 fizz buzz Since this look right 12 fizz for buzz says 78 fizz buzz 11 fizz 78 fizz buzz 11 fizz 13 14 and then fizz buzz for sure. Okay, so this looks good. So just try to run this ruby fizz bows Solution. Nurse, I'm hitting Tab here again, Okay. And this looks good to me. Okay, Um, so you mentioned a few other ways you could do this. So other ways with arrange retreat just saw so one up to 100 you could say each year now If we run this again, we get the same result. It's the range worked good, have also said to a really and that would work to was really no reason to do it here. But you could do it Another thing. And one thing that trips up a lot of people is that they want to be overly clever in this section. So one thing you might think to do is say, if numb is too visible by five. Basically, you just want to do this print buzz right? So you could have separated if statements but constitute in the opposite order. So it's divisible by three print fizz is divisible by buzz are divisible by five print buzz . And then, in our last case, we need Teoh print the number. But, um, is this might be more arrogance wishing, but we don't want to print the number if it falls into either of these cases. So one way we could do this divisible by three is that and divisible by three and Adam, by the way at all show potential options so you can click. But I prefer to just up and down arrow So and then the other one is basically the same thing that won't just call five, and I'm copying and pasting these instead of trying to change it. Because you know it's easy enough to create Type O and then you have to spend time toe kind of find it. So it's just easier toe copying pace. But you could also take up so you could say if divisible by five. And so we could actually say if it's not divisible by five and it's not the visible by three, then print the number. So this might be another way to do this. Let's try it out. So one to fizz for buzz. 70 fizz buzz. Now check out 15 though 15 is wrong. So this is an interesting case where this doesn't produce the same output because of our puts. So there's one way we might change it. So here's our help. Did we say our output? And by the way, this is similar to an array. We're gonna just append it. So he had a string. You could just a hand something. So now you're string looks like this. So this is one option so we could do this. And now let's see what happens So one to these look, okay. And then 15 looks OK, so this also works, But you have to think about what is the most obvious If I'm coming to this program. I've never heard of fizz buzz before. Is this code more obvious, or is the old code more obvious? So either way will work, But But ah, I think the 1st 1 is somewhere. So I'm just command z undoing. So I'm just gonna commands the end you until we get to this, which I really was the right solution as well. This was the right solution. Okay? Also, just so it'll thing that I figured out that I actually learned from someone is this is media Mac specific thing is that if you speed up your key repeat rate in your delay until repeat, you can undo really, really quickly. So when you make, like, big changes like this can basically go back really, really quick. That's just a little tip. There's probably a way to do that on the windows and winex. Um, I've only learned how to do it on. I was 10 but I'm sure there is way to do it. Just Ah, little tip toe. Help! Speed! You Okay, so that's the basics behind fizz, Buzz. And in the next section will be talking about functions and their arguments and Lando's and all sorts of good, all sorts of good stuff. All right.