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

Ruby Tutorial 5 - Creating a Script


6m read
·Nov 3, 2024

Hey guys, this is Maads 101 with our fifth Ruby programming tutorial. In this video, we're going to be writing our first actual script, which is a Ruby program that runs without us typing things into the Ruby console.

So, in order to be developing a script, we're going to use two programs that both come pre-installed on your computer. One of them is Terminal, which we've been using for the previous tutorials, and the other is TextEdit, which will allow us to edit our script in a pretty easy and straightforward manner.

To get to TextEdit, in Terminal, in Finder, I'll go up to the Go menu, go to the Applications folder, and then scroll down to find TextEdit and open it up. Then, if you go to Utilities, you'll find Terminal.app and open that up as well.

Now that I have TextEdit and Terminal open, I'm going to show you how to set up both of these apps in order to run a basic Ruby script. I'll explain to you a little bit about what a Ruby script does.

In TextEdit, the first step you need to do is change it to plain text. In order to do this, go up to the Format menu, then go down to Make Plain Text. Now, as you see, the little format bar at the top of the TextEdit window disappeared. So now we're editing a plain text file, and this text file is going to be our Ruby script.

Let me just enter a simple line of Ruby code. I'll just make it "hello" because, okay, and now let's save this as a Ruby script. In order to do this, you would hit Command + S or go up to File, Save, and I'm going to call it test.rb. You can technically call it .txt, but I suggest writing .rb there because that's the standard file extension for Ruby code.

Now, hit Enter, and it'll save onto my desktop, as you can see I've got it right here, and this is our Ruby script. I'm editing it in TextEdit. Now, let's run it in Terminal. In order to run a Ruby script, right after you've opened up Terminal, you have a fresh prompt and everything you run, you run it with the Ruby command.

To do this, just type "ruby", space, drag in the script, and then hit Enter. As you can see, it says "hello" and the script has "hello" in it, so clearly it ran the right script.

Now, let me get into a little detail on what just happened and how we can write more advanced scripts. If you'll recall, if I throw up IRB and I were to type puts "hello", you would see "hello", and then you would see a return value and then you would see a prompt, and you'd also see the code I wrote.

Well, when you run a Ruby script, you don't see the code because the code is in the script. You don't see it print out in the terminal. As you can see here, you do see the output of puts and stuff like that, but you don't see the return value.

So, let's say I were to write 1 + 2 in a script. I wouldn't see this because three is the return value, and I wouldn't see this. So 1 + 2 would actually have no output; I wouldn't see anything come out in the terminal if this line were to be executed in Ruby code in a Ruby script.

The purpose of a script is to basically make Ruby code that runs that the user can't really see unless you print something out or you get something from the console using gets. That's the purpose of a script: to write a program that interfaces with the user using puts and gets basically.

Now I'll type "quit" in IRB to get back out of that and get back to our prompt. So that is basically what went on with this little puts program. Now let's recreate that script that I made in the last tutorial so long ago that will ask you to enter your name and then say hello to you.

So it'll say "Enter name," and then it'll say "name equals gets.chomp," and it'll say "hello, name." As you recall, this will just embed the variable into the puts. This will get the name from the console and get rid of the new line, and this will tell them to enter their name.

Let's go ahead and save this. Now, let's run it by typing "ruby", space, and then dragging in and hitting Enter. So now it's asking me to enter my name, and as you can see, it just says "Enter your name." It doesn't say "name equals gets" or "puts enter name" or anything like that; it just says what I put as.

I'll type "Alex," and it says "Hello, Alex." So as you can see, this looks much nicer than what we got with our IRB console. We ran the same code because the user wouldn't have had to type in the code as it went along, as opposed to in the script where it's already in the file.

Also, you don't see return values cluttering stuff up, etc. So the idea with the script is that I could take this file, send it over to my friend, and he could run it in Terminal too using the same Ruby command. He would see the same program, and he wouldn't actually have to know Ruby. He would just have to be able to interact with this, type his name, and hit Enter.

Now, let's go ahead and write a slightly more complex script that will utilize the features slightly more. We're going to make a program that adds two numbers together based on what the user types in.

So first, I'll show you what you might want to try to do, and then I'll show you how to actually make it work. Because this is actually like a developing thought process. Whenever you write a program, you have to think about what you're doing.

So, obviously, the first thing we're going to need to do when writing this program is ask the user to enter two numbers. So we ask them to enter a number, and we say "number_one = gets.chomp." Let's say, and then let's ask for another number.

All right, and now we're going to need to print out the result. Now let's go ahead and first get a variable that should hold our result. So let's call it "sum," and we'll make it "number_one + number_two."

Now, this won't work, and I'll explain why in a second, but let's go ahead and run it. The sum is "sum." Let's save this and run it, and I'll show you what will go wrong. I'll enter a number; let's say I write 10, and then let's say I write 15. The answer should be 25 when I hit Enter here, but as you'll see, it's "1,5."

Now, it's not actually a number. The answer is not this, and this isn't actually a number. It just took what I entered here and took what I entered here and put one next to the other and printed that out. That's because, let's say I go back into IRB, and I type "10" in quotes plus "15" in quotes; it appends them, and you'll get "1015."

Now, in order to basically convert these to a number that we can add, you're going to use the .to_f method, which will convert them to a floating-point number. This will allow you to add them like you normally would.

So you'll put .to_f after one string and .to_f after the other string, and you would get 25 as a number. Now, let's go ahead and essentially do this in our script. All we have to do is put .to_f after this number and .to_f after this variable.

So you'll end up getting the strings which were input using gets, which will always input a string. Ruby doesn't know if the user's going to enter a number or a name or anything like that, so it's just going to treat it as a string since a string can really contain anything.

But then, you know that the user entered a number because the purpose of your program is to add numbers. So you can basically use the method that gets a number out of a string to convert them to numbers and then to add them using the normal plus operator, and that's what we're doing.

So now let's go ahead and save this and then quit out of the IRB console and then run it using the Ruby command. I'll enter number 10. I'll enter 15, and it'll say 25.

So that is a pretty straightforward example of a script that you can do using pretty much all the stuff we've learned and how to edit and run it. In our next video, we'll be covering more stuff. We're going to be using scripts basically from now on, and we're going to be writing scripts, more complex scripts, maybe even programs with multiple files involved in them, but that'll come much later.

But anyway, thanks for watching, stay tuned, and goodbye.

More Articles

View All
The Mystery of Synchronous Fireflies - Smarter Every Day 274
Hey, it’s me, Destin. Welcome back to Smarter Every Day. I grew up here in Alabama, like most kids in the area, learning how to catch lightning bugs and putting them in a jar. It’s a magical memory that most of a share. But today, on Smarter Every Day, we…
How To Get Rich According To Warren Buffett
There are a million ways to make a million dollars. In this video, we’re looking at one of them, and the main character in this video is the legendary Warren Buffett, who made his fortune of over 104 billion dollars by investing in the stock market. After…
How Damaging is Radiation?
What is radiation? Isn’t a bad type of poisoning. It’s just like a dirty word to me. It’s just something which is not good, not good for me, being a human being exposed to great amounts of it—waves of bad stuff. Yeah, I mean, it’s dangerous. We all know …
2015 AP Physics 1 free response 3c
All right, now let’s tackle part C. Use quantitative reasoning, including equations as needed, to develop an expression for the new final position of the block. Express your answer in terms of D. All right, I’m going to set up a little table here for par…
How Your Amazing Brain Tells Time: Circadian Watch, Pattern Pendulum, & Tempo Timer | Dean Buonomano
So human beings have been building clocks for millennium, and it’s been a long endeavor of our species from sundials to hour glasses to pendulum clocks to quartz watches to car and atomic clocks. Yet the brain has been telling time since the dawn of anima…
TIL: Almost 40 Percent of New Yorkers Are Immigrants | Today I Learned
So get this, there’s more than 3.2 million people in New York City that were born outside of the United States. Oh, that makes New York City, by a wide margin, the city with the most foreign-born people of any other city in the world. I’m Jar Thorp. I’m …