This article is a basic collection of some useful ruby libraries with example. Here I will talk about abbrev, benchmark, chronic, erb, faker and redcarpet. We can use these for adding up good features. And improvements for our applications. Some of these are available with ruby standard installation, while others are available as a ruby gem.
The abbrev library offers a single method. That calculates a set of unique abbreviations for each of a supplied group of strings. This method returns a hash. Where the keys are the possible abbreviation and values are the full strings. It is available with ruby installation. For using this you need to load this library. It is useful, in case of giving the user a hint for close typos or auto-complete suggestion.
Benchmark tool is also part of the Ruby standard library. Developer use it to measure the performance of any method or block. It gives CPU time, system CPU time, the sum of the user and system CPU times, the elapsed real time. The unit of time is seconds. It is useful when we need to decide, and see which method is more efficient in case of heavy operations. For example. As shown below, here we want to see which loop is taking less time while executing.
Chronic is a very interesting library. It accept dates and times. That written in natural language format and return valid Time objects. The chronic library isn’t part of the Ruby standard library, and is available as a Rubygem. It is very simple to use. We can use it when we need to parse some user’s answers. Example parse answers from some chat bot and store the same in date time object in our databases.
Chronic.parse will return nil if a date or time isn’t recognized.
ERB stands for Embedded Ruby. It is a templating library for Ruby that allows you to mix content. And Ruby code, that means embedding ruby code into a text document. ERB is used as the main template system in Ruby on Rails. when rendering HTML views (Files extension be like test.html.erb). It is a part of the standard library, so it comes with Ruby by default. Ruby codes be written as an expression, like <% here goes ruby code%>.
This Library is a very useful one for generating fake data. It is mostly used while writing unit tests to add dummy test data and validate formats. Additionally used to fill some dummy data in staging or development environments. It has a lot of universal features, including email, phone, and facsimile.
Redcarpet is a Ruby library for Markdown processing. (Markdown is a way of writing text that can be converted into formatted HTML). These days, Markdown is widely used for all types of documentation. This is used to accept Markdown in text fields so that the user can view it in properly formatted, readable HTML. This is available as a rubygem.
Hope after reading this documents, you would have got some good knowledge about some of the simple but useful ruby libraries with example.
Please do let me know your feedback. Or ask questions via commenting on the below comment box. More reading more similar blogs please visit https://engineering.rently.com/ruby-on-rails