Here at creditas we love Ruby. We have squads working with both Ruby on Rails and Hanami. When we are developing a web application using a framework, such like RoR, we find ourselves discovering lots of new features that the framework give to us, for exemple: ActiveRecord Validations and Migrations, ActiveModel Serializers and Callbacks, and the list goes on.
A few weeks ago we found ourselves with a requirement to produce eleven digits random numbers. As we searched for some solutions we discovered the ‘SecureRandom’ module, inside the Ruby standard library. It did not work for our requirements, but I started to search for interesting things inside the documentation and was able to find some peculiar modules in there.
So have you ever dived into the Ruby standard library? You might be thinking that it has only basic and ordinary stuff, like CSV module, for example. This post is here to show that you would be surprised with the hidden stuff inside the Ruby Standard Library. Let’s start with the DBM module.
Did you know that Ruby has a Database Manager Library that can store data in key-value pairs? If not, now you do. The DBM class does not store you information in RAM, like a hash does, instead it stores data in files, and you can use it like this:
First you need to require the dbm module. After that just open a DBM database with the filename, an integer value for unix chmod, and the last attribute says that it will open the dabatase with read and write privileges, and create it if does not exists.
DBM is great, but no so great when you need a long term storage tool. And it has some cautions before use, as you can see it here: https://ruby-doc.org/stdlib-2.5.3/libdoc/dbm/rdoc/DBM.html
So if you need something easy and fast to store data and get your project going DMB might be a good choice.
The DBM package was a good discovery, and it is a useful class for some cases. In other hand, the ETC module is here to prove the title of this post. This module is used to access information found in the /etc directory on Unix systems. Why would you use that? I really don’t know haha but it is up to you to figure it out.
With the ETC module you can do stuff like this:
The list of peculiar methods like this goes on, if you want to search more here is the link: https://ruby-doc.org/stdlib-2.5.3/libdoc/etc/rdoc/Etc.html
This is an interesting module, because the Coverage module basically provides coverage measurement feature for Ruby. But is it like a test coverage tool? Not quite, the coverage class gives, for each line, the number of line execution by the interpreter . Let’s look at an example:
Therefore, with this example we can see how many times each line was executed by the Ruby interpreter. For lines 6 and 7 we can see that it returns nil, it means the coverage is disabled for this lines. This feature could be really useful to find dead code, for example.
The documentation says that still is an experimental feature, so if you want to look more here is the link: https://ruby-doc.org/stdlib-2.5.3/libdoc/coverage/rdoc/Coverage.html
To finish we have a module as peculiar (weird) as the ETC module. As the docs says, the Abbrev module calculates the set of unambiguous abbreviations for a given set of strings. What? Let’s look at an example:
Why would I use that, you might ask. I really don’t know, but if you know some use case share in the comments :)
That’s it everyone, I am pretty sure that exists some other peculiar and weird methods inside the Ruby standard library, those are the ones that I was able to find. Share in the comments if you were able to find some others.
Thanks for reading :)
Our goal in Product Technology is to create the best solution to our customers’ needs by offering the most complete and innovative portfolio of financial products from financing to investing. We’re here to build a fully automated and intelligent digital platform and we’re looking for people who’ll join us on that: https://jobs.kenoby.com/careers-creditas