Using Libraries

Learn about Ruby libraries and how to use them.

Our Person class doesn’t define an attribute accessor method for its password, and therefore, others can’t ask for and retrieve it.

However, our person object could freely give them an encrypted version of it.

This is pretty similar to how authentication often works in real web applications:

Applications don’t store our actual password in plain text (hopefully). That way, if they get hacked, attackers wouldn’t have our actual password. Instead, they store an encrypted version of the password.

We now want to add an encrypted_password method to the Person class. The method should return an encrypted version of the password that’s stored in the @password instance variable.

Encryption is one of the things in programming that requires very deep expert knowledge, and it’s one of the things we definitely wouldn’t want to implement ourselves.

Introducing libraries

So far, all Ruby features and methods that we’ve used are available right away when the Ruby runtime, ruby, executes our code. However, Ruby also comes with a ton of functionality that’s not available (loaded) right away. Instead, it’s stored in so-called libraries (just Ruby files too), and we have to load them manually to make them available.

The require method

To do this, we use the require method and pass it the name of the library:

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy