yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Ruby Tutorial 2 - Some Easy Methods


6m read
·Nov 3, 2024

Hey guys, this is madquez101 with our second Ruby programming tutorial. In this tutorial, I'm going to be showing you a couple methods on numbers and strings that are really useful, that you'll find yourself using a lot, and that you really have to understand.

So a method is essentially something that you can do to an object. Let me just go ahead and introduce you to a couple methods, and hopefully you'll get the idea pretty quickly. So I'll go ahead and run irb, and if you'll recall, if you just type in negative 10, something like that, negative 10 will be returned. That means that negative 10 is essentially an object. So when I write negative 10, that is an object.

Now, since we can call a method on any object, we'll go ahead and call a method. The method I'll be showing you is called abs. So, to call a method, you have the object, then a dot, and then the method name abs. The abs method stands for absolute value. If you don't know what that is in math, basically the absolute value of a negative number is the positive equivalent. So, the absolute value of negative 5 is 5. The absolute value of a positive number is just the number.

So anyway, let's go ahead and hit enter, and as you can see, the absolute value of negative 10 is 10. That is what we were expecting. Now, a lot of people are probably thrown off right now because the dot is used for decimal places in floating points, and it's also used for calling methods. So when you see something like negative 5.5.abs, you think, "Oh, well how does Ruby not know that 0.5, or how does it know that 0.5 isn't a method call?" Right? Because 5 could be a method name.

And the truth is that 5 couldn't be a method name because method names can't have numbers at the beginning of them. They can't start with a number. So if Ruby finds a period and then a number, it knows that it's not a method. It knows it's part of the floating point. So if we type negative 5.5 dot abs, it'll take the absolute value of negative 5.5. So we hit enter, and we get that.

Now I can understand that some of you will probably be confused about this; I was confused at first, I have to admit. So, what might help is if you put negative 5.5 in parentheses. If you recall, if you just run negative 5.5 in parentheses, it'll just return negative 5.5. So it's basically the same thing. So if you do this.abs, it'll get the same thing. That might just help you understand it more. It might confuse you more, I don't know.

But anyway, that is the abs method. You can even call it on a positive thing, so ten dot abs will give you ten. You can call the abs method on any number, and some other objects might support it as well, but I won't get into that.

Alright, so now I'm going to be showing you the round method. This is mainly, you're going to be using this on floating points. So if you recall, a floating point doesn't have to be a whole number; it can be something like 3.14. But let's say we want to round that. So we want to round 3.14 to the nearest integer, and if you've learned this already, most of you probably have, rounding 3.14 to the nearest integer will give you three. The method for this is called round. So, you guessed it, to call it we just do 3.14 dot round, hit enter, and there we go, it's just three.

Uh, it also will round up. So if we type 3.5 dot round, it'll give us four. So that is the round method. Now, it also works for negatives. You know, you can even do something like 3 minus 6.7, and I'll put that in parentheses, and then I'll have dot round, and it'll give you the rounded negative, all that stuff it did for you, so that's pretty crazy.

So that's the round method; it works on any object. Now let's go ahead and look at two other methods that are kind of related to the round method but aren't the same thing. Let me just go ahead and show you one of these: 5.5 dot ceil. Now ceil stands for ceiling, and that's because it'll basically round up. It'll always round up, so 5.1 rounded up will be six; 5.5 rounded up will be six. Let's go ahead and do it; it's six right now. Five rounded up, since five is an integer already, it'll just be five.

But if we have anything greater than five and less than six, like 5.0001, we'll get six. Alright, and there's also basically the converse of this that'll round down. So let's say we have 5.9 dot floor. Now, you probably are noticing a pattern: ceil for ceiling and floor; it's pretty obvious. So, 5.9 floor, it'll just give you five.

An easy way to think about floor is it just knocks off whatever's after the decimal place, and that's what rounding down does basically. You can also do this on an expression; so eight plus zero point one dot floor will just give you eight, because eight plus zero point one is just going to be eight point one.

Uh, hopefully most of you guys are still on board with me here. If you have any questions so far, just go ahead and leave them in the comments, but I'm going to go on and show you some more methods on strings. So you can call methods on any object; it doesn't just have to be a number.

We also learned in the last tutorial about strings. So if I have "abc" and hit enter, the object that's returned is a string, right? So I can call a method on "abc," and one of the easiest methods that you can call on a string, and this is the first one we're going to be learning, is called length. So dot length. And if I go ahead and hit enter here, it'll give us three; that's because "abc" is three characters long.

Alright, now there are a couple other cool ones. How about "abcdefg" dot reverse? Reverse does exactly what you'd expect it to do; it reverses the string. So the last letter is now the first letter, and it reversed everything in between. Really great, so that is the reverse method.

These are just some of the many methods on strings. I'll just show you a couple other ones since some of you might be interested. Let's say I have my name, and usually, you're supposed to capitalize the first letter of a name. So in order to do that, you can use the capitalize function or method, and it capitalizes the first letter of the string.

Um, but let's say I have "alex," and okay, I really want it to be all uppercase. I can just type "upcase," and it's all uppercase. I can just type "downcase," and it'll go all lowercase. So those are two other methods on strings, or three methods on strings that are basically essential to casing and changing the case of strings, stuff like that.

Those are really useful; I actually find myself using those on a day-to-day basis, so you should definitely know those. Let me just show you something that might confuse people: how about we type "abc" plus "def" dot reverse? And if you'll see, you hit enter, it's "abc" fed. This is because of the order of operations.

So "abc" gets appended to "def," and this is how people might think about this: "Oh, I'll just append 'abc' to 'def', and then I'll reverse that." But there's an order of operations here that's going on, and what happens is the reverse method gets called before the plus gets processed.

So it reverses "def" first, and then it appends it to "abc." So there's an order of operations there, and most of the time, method calls will happen before other operators. So you can also see that like one plus or one minus 1.5 dot floor comes out to zero because it's one minus one, because 1.5 dot floor is just one.

Alright, so that is essentially the order of operations when it comes to function calls. So my rule is when in doubt, use parentheses. You put parentheses around "abc" plus "def," sorry, and then you have a dot reverse after that, and you'll get the whole thing in reverse. So that is the better way to do it if you're expecting a certain order, and you forget what order might happen, just use parentheses and you'll be safe.

So that is just an introduction to some basic methods on the data types that we've learned so far. If you have any questions, I'm sure a lot of you do, go ahead and leave a comment. Check out our other videos if you haven't already.

So thanks for watching, subscribe, and goodbye.

More Articles

View All
You Don't Type Alone.
Hey, Vsauce. Michael here. And thank you for clicking on this video. But how many times a day do you click? And how many times a day do you type keys on a keyboard? You might be surprised by the answer. And one of the best ways to know exactly how many ac…
How much money I made from 1M views- How to make money on Youtube
You probably saw YouTubers buying luxury cars, designer clothing, and expensive houses. And I’m pretty sure that you have at least for once wondered how much do these YouTubers make. So in this video, I’m gonna show you exact data of how much money I made…
404, the story of a page not found - Renny Gleeson
[Music] [Applause] So what I want to try to do is tell a quick story about a 404 page and a lesson that was learned as a result of it. But to start, it probably helps to have an understanding of what a 404 page actually is. The 404 page is that—it’s that…
Stop Looking For The Success Formula
Hello Alexa, welcome to Honest Talks. This is a series where we talk about things that we personally find interesting, and we think you might too. Today’s topic is how to craft your own success formula. So these numbers, they were worth millions of dolla…
How to sell a $23,500,000 private jet | Dassault Falcon 7X
This is a Dao Falcon 7X aircraft. We’re going to take a look through it right now. So, Steve, I’ve noticed that this aircraft has three engines compared to the other two-engine aircraft. Why is that? It’s quite funny, there’s three engines versus two en…
Saying no is hard. These communication tips make it easy. | Michelle Tillis Lederman | Big Think
MICHELLE TILLIS LEDERMAN: “No” doesn’t feel so good. We feel a little uncomfortable. We feel bad saying no. “No” to something is “yes” to something else. And that’s the first thing you need to think about to give yourself permission to say no. My husband …