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

Ruby Tutorial 1 - Numeric and String Operations


6m read
·Nov 3, 2024

Hey guys, this is Mac Heads 101 and today I'm going to be doing the first of many Ruby programming tutorials.

Now, if you're not familiar with Ruby, it's basically a programming language that is used for scripting. You can use it for web development with Ruby on Rails. Um, it's very good for a lot of things. It's a good multi-purpose language and it's a good language to start out programming with. So, it's a good first language for those of you who haven't programmed before.

I'm going to be targeting this string of tutorials towards people who have not programmed in the past. This is their first time, so it's going to go a little bit more slowly than some other tutorials I've made, but I think it's for a good purpose. You know, and Ruby is a great language to start learning with.

So, uh, in order to be programming in Ruby, we're going to be using an application that comes with Mac OS 10 called Terminal. You can open up Terminal by going to the Finder menu, going up to Go, Utilities, and then hopping down to Terminal. Opening it up, it comes with every computer, so you should just look on the list and it'll be there. Alternately, you can search for it in Spotlight, and that's all good.

Anyway, here is Terminal and let's get right ahead to a place where we can start typing Ruby code and, uh, seeing what it does. So we can play around and experiment with Ruby, uh, and hopefully that'll help you guys start to learn some Ruby. So, uh, in Terminal, we'll type IRB and hit Enter. All this does is it runs the Ruby interactive console.

So now you'll see these two greater-than signs right here. That's our prompt and that's basically Ruby telling us now we can enter a command or some Ruby code and it'll tell us the output of the code. So in general, every expression in Ruby has an output. So if I type 1024, for instance, and I hit Enter, it prints out the output by having little equals and then a greater sign, and the output is 1024. It's a number.

So basically, we put in 1024, we get out 1024. That is the most basic expression that there is in Ruby. So it's very important that you understand that. Now, you can also do, uh, stuff like arithmetic operations and this is just integer to integer. So an integer is a whole number; it can be negative or positive, but these are just integers for now.

Um, so if I type 10 plus 10, it'll print out 20. If I type like 10 times 3, it'll be 30. I can do 10 times 3 plus 2 and it'll be 32. Right, all the things we'd expect. You just do times by typing a star, so 10 times 2. You could do 10 slash 2 and that'll be 10 divided by 2.

The order of operations applies, but you can also put in parentheses, so you can have 10 times 1 plus 1. You know, if you put in parentheses, it'll be 20. If you didn't put in parentheses, it'll be 11, right? Because the order of operation says that multiplication should go before addition, you know, stuff like that.

But anyway, that is, uh, some basic integer math with Ruby. Now, you can also use something called floating points, which are decimal numbers. So for instance, 3.14 is a floating point. If I type 3.14, the output is 3.14. Now, it might look kind of the same because you're used to seeing numbers this way, but if there's ever a decimal, it indicates that that's a floating point. Otherwise, it's probably an integer.

So if I do 3.14 times 10, it'll be 31.4. Okay, now 10 is an integer and 3.14 is a floating point, so we can mix them around. But if I have 10.0, it'll be the same thing. So you'll notice if I just keep everything as integers. So I have 10 divided by 3. It'll just be 3.

Now that's wrong because clearly 3 times 3 is 9, not 10. But because it's integers, it can't display that much precision, so in order to make this use floating points, we have to type 10.0 divided by 3.0, and then it'll give us the right answer or a closer answer since the answer is decimal points that go on forever, you know? But in this particular case, but anyway, that is the difference between integers and floating points.

Now with both integers and floating points, all of these basic operations work. You can multiply, divide, subtract, add. You can also use exponents. For instance, 2 star star 3. How about star star 3? That'll be three to the third power, so if we hit Enter, it's 27.

So the double asterisk is an exponent, and this all follows the order of operations. You can throw in parenthesis or anything you want. Um, so that's, it's, uh, really helpful. Anyway, that is arithmetic operations in Ruby. Now, these are some basic concepts that you should already understand.

Kind of if you type a number, that number's result is going to be the number that you typed in. And then you can put these operators between any number and any other number, and you can also use the parentheses. But you can also add pieces of text, and let me explain to you because this sounds very funky.

When you're writing or doing something and you want to quote something, you have a quote and then some text and then a close quote. Well, in Ruby, you can do this and it's called a string. So if we have a quote and we write "test" and then another quote, hit Enter, the result is the same thing we typed in, just like if we were to type in a single number. The result for typing in a single string is the same string that we typed in.

But we can also add strings, so let's say I type "A" in quotes and then I have a plus and then I have "B" in quotes. Well, you know, maybe some people might say, "Oh, the output will be C," and that's actually wrong. What will come out is "AB." So what happened here? Basically, the plus two strings, when you add two strings, it takes the first string and then puts the second string at the end of the first string.

So if we have "this is" plus "a test," it'll say "this is a test." Now we didn't put a space in there, but we can also do that, like just put a space at the beginning of the second string, and there'll be the space. And what you just saw me do there was I hit the up arrow and it brought me to the last command. You can keep on hitting up or down to go between those.

Um, but anyway, you can basically add strings together. You can't really subtract strings or divide strings; you can have a string, like let's say "A," and then I have "A" times and then I have an integer, so "A" times 4. Now some people might say, "Oh, it'll be D." Once again, that is wrong. What it'll actually be is "AAAA." It'll be "A" four times.

So you can multiply a string by an integer and you can add two strings together, and that's the bare essentials. You can mess with the other possibilities; you can mess with this to see if there are different equations or expressions that you can type in and make Ruby do them.

So this has just been our first Ruby tutorial. To get out of this, you just type "quit" and you'll be back to your normal terminal prompt. And then to get out of that, you'll just type "exit" and close the window.

Right, so anyway, that is how you, um, do basic arithmetic expressions in Ruby and, uh, then some strings. In the next tutorial, we're going to be going into depth, uh, messing around with strings, maybe printing out some strings, doing some stuff like that.

Um, so stay tuned. Um, I've been programming for several years and, you know, this is supposed to be a beginner tutorial, and sometimes I find it difficult to understand what you guys have trouble understanding, if that makes sense.

So in the comments, I would love it if you guys would give me feedback. You know, what do I need to do better? What don't you get? You know, I'll answer all your questions, anything like that. So, uh, thanks for all your support. Um, hope that you enjoyed the tutorial. So thanks for watching, subscribe, and goodbye.

More Articles

View All
Why I'm ALWAYS broke by the end of the year…$300,000 gone
What’s up, you guys? It’s Graham here. So, this is this weird investment strategy and mindset I’ve been practicing since 2011. Now, maybe it’s a little bit weird, and maybe it’s a little bit risky, and maybe it’s a little bit stupid, but this has been wor…
Predicting bond type (metals vs. nonmetals) | AP Chemistry | Khan Academy
In a previous video, we introduced ourselves to the idea of bonds between atoms, and we talked about the types of bonds: ionic, covalent, and metallic. In this video, we’re going to dig a little bit deeper and talk about the types of bonds that are likely…
Terminal Lesson 3
Mack heads 101. Today we’re going to show you how to use terminal. This is from Ana lesson three. We’re going to show you how to change another user’s password or your own password, how to shut down the computer, how to restart the computer. So first of …
Worked example: limit comparison test | Series | AP Calculus BC | Khan Academy
So we’re given a series here and they say what series should we use in the limit comparison test. Let me underline that: the limit comparison test in order to determine whether ( S ) converges. So let’s just remind ourselves about the limit comparison te…
Top 7 WoW Cataclysm Visual Changes -- Wackygamer
Hey guys, Jeff here from Wacky Gamer. I wanted to bring you the top 7 things I’ve seen in the latest World of Warcraft Cataclysm footage. They finally filled in Thunder Ridge outside of Orgrimmar. I don’t know what Blizzard was thinking, but it’s like, l…
How to lead remote teams: The Navy SEAL playbook | Chris Fussell | Big Think Edge
I’d like all of us to think about science and its methods and tools: space-time, the nature of human consciousness, artificial intelligence, incredibly realistic humanoid robots, a new technology for gene editing, the battle against climate change, and th…