Ruby is more than just a language, it is a whole ecosystem. The power of the language is greatly multiplied by the thousands of gems freely available from the public gem server, but there are also some amazing tools that come already bundled with Ruby.
In this course, you'll learn how to use the standard libraries that come bundled in with every installation of Ruby. Tuts+ instructor José Mota will teach you how to:
- manipulate YAML, CSV and JSON data
- apply delegation for better code reuse
- fetch content from the web using
Net::HTTPand the URI library
- build Rake tasks
- manage files
You'll be amazed how much can be accomplished using only the standard Ruby libraries! Each library will be taught with simple examples and exercises that you can adapt for your own programming problems.
1.Introduction1 lesson, 01:51
2.Ruby Standard Libraries12 lessons, 1:46:56
3.Conclusion1 lesson, 00:56
Ruby is more than just a language. It is a whole ecosystem. And part of that ecosystem comes a set of different tools. Aside from the thousands of gems already in the public server, there are some tools that come bundled in when installing Ruby. Hi, I'm. I'm going to be your tutor for this Essential Ruby Librarie course. All of the libraries you will see being used in this course are part of the standard library in every Ruby distribution. You will learn how to manipulate certain kinds of data with CSV, YAML, or JSON. You will also learn how to apply the concept of delegation through two very simple libraries. They will aid you in developing better code. You will also learn how to fetch different contents from the web using the net http library along with the URI library. At the end, we'll learn how to build a task, along with some file operations. This will be a very interesting course, in the ways that it will explore some things that you never thought they would be available right away. I hope you're excited about it. This course assumes that you have a basic knowledge of Ruby already. If you don't, go ahead and check the Learn Code with Ruby course. This course covers all of the basics, from installing a Ruby version to the syntax and some tools in order to run Ruby code. After that, feel free to jump back in. So I hope you're excited about it. Let's jump into the next lesson straight away. We'll learn how to manipulate some data with CSV.
2. Ruby Standard Libraries
The first library that I wanna cover is actually MiniTest. MiniTest is a testing framework that is actually part of the standard library. So I'm gonna give you a quick introduction on how to test in Ruby with MiniTest. The way you do it is with a simple file that contains a class much like any other piece of Ruby code. The first thing that we're going to do is requiring MiniTest. Actually, MiniTest is the library, but if you want to run tasks automatically, we use MiniTest/autorun, as the name implies, it does exactly that. It runs the tests straight away. So we're going to require that file in specific and we're going to start by creating a simple test case. Let's create a demonstration test really quick. This will inherit from MiniTest:: test. Let me take the chance and save this file right away. I'm just going to create a demonstration_ test that'll be filed in the root of the project. Now let's go back to the test. As you can see it is just a straight up class. Nothing really too big. Next we're going to use a method, and the method will be our test. It must always begin with test_ ,and then whatever name you want to give it. For example, the classic asserts the truth. This is a classical example in testing, because we want to assert true, something like this, asserttrue. Now, in case you don't know what this is, this is called an assertion, assert is a variable that will evaluate the condition on the right side. If it evaluates the true event the test is green. The way we run this test is actually quite simple, all we need to do is type in Ruby and demonstration_test. If I press enter you will see the test sweeping run. We have one test that is actually green. Okay. No big deal there. So what happens if I create and insert false statement? Because false is not true, this condition will fail, and you will see a detailed message on what's happening. It says failed insertion, no message given. The part of the message comes as a second argument in the assert method. So the first argument is the condition to be evaluated, and then you can pass any message. For example, we expected true, not false. Running this test again, let me just type in clear first and then ruby demonstration_test.rb. Now you can see the message. We expect a true, not a false. So this is a classic example, it's really that simple, in order to demonstrate how a test is structured. You require minitest/autorun, then minitest::test, this is the class it's going to inherit from. Remember that you'll type your own class name. This is the one that we've given for this one, and then for each different method you will have a test. Remember the tests must begin with test_. Now what if you wanted to create a little more complex test? A test that asserts on our own classes. Lets say for example, that we have a person class. I'm going to create it right away and I'm just going to leave it at this. I'm going to create a standard testing session. Let me just keep the assert true here and create another test, so test_person_has_a_name. Let's say that we want to make sure that the person class will have a name attribute. We'll create a new person, which will be a new instance of person. And it will have a name, for example, my own. And then we want to assert that the person's name is that one in specific, like so. Notice how we are using assert_ equal. This is a new kind of assertion that tells whether the first argument is exactly the other one. When we do this, we will have an error, naturally, because let's just go ahead and try that. As you can see, wrong number of arguments is presented to us. A standard testing session will involve a lot of trial and error. Because that's what testing is all about. We crawl our way forward into perfection. Because the person class expects an argument and we don't have it, it's going to complain. So in line four, we'll create an initialize method that takes that name. Then, we'll instantiate the value into an instance variable and now we can go to the right side again and run the test again. Again, standard procedure. Fix the code, run the test, until you reach a green state. So, undefined method name map. What does this mean? Well, we just have the instance variable, but it's not exposed to the outside. So, what we do is create an attr-reader with the name. This will expose the instance variable like so. It's basically the same thing as typing a method called name and returning @name. It's exactly what edit reader does, but in a simple way. So we have that covered. Let's run the test again. And it goes green this time. Okay. Let's sum this up really quick. Require minitest/autorun to have the functionality available, then you instantiate the tests that you want to have. In this case it is called demonstration_test. And for each test you create a method that begins with test_. The body of the method is basically the entire setup around your test, around your need. My need is to have a person object on which I want to search something. Person.name is the thing that I want to compare to my expected value. The assertion depends on what you want. I used assert on the first one, which basically evaluates everything to true. And then assert equal, compares two different arguments. I'm gonna give you a list of all of the assertions available in MiniTest in a link to the description below. So make sure to check those out. Also, if you want to check the official documentation and repository for MiniTest, you should check this link. It's also in the description. MiniTest started out as a group initiative from Seattlerb, but then it was adopted by the Ruby standard library, and so it comes bundled in already. So make sure to check the readme, it has loads of different examples on how you can use MiniTest. You can see that there are many different components into MiniTest, so give those a look.