Ops Scripting w. Ruby: Frequency

Tracking Frequency in Ruby: Part I

Joaquín Menchaca (智裕)
2 min readMay 13, 2019

--

Automation is big part of operations oriented roles, and ruby is a popular scripting language. Previously, I did a series on doing this in Python; now this is the ruby version on how to create a frequency hash to track the occurrence or duplicates.

In our series, we’ll use a supplied passwd file and count the shells, and show how to create the hash dynamically with a collection loop, and serially using collection loops and how to convert these to functional programming style with map() and select().

The Problem

The goal of this exercise is to get a frequency from a file, and we’ll save the results in a frequency hash. This may be academic, as Ruby has a built-in count method (which we’ll use for more advanced solutions). This exercise will illustrate how to use features within the ruby language, as well as have a common comparison point to other languages for this problem.

For this problem, we’ll print a summary with the shell and the number of users of that shell, using a local copy of the /etc/passwd. We’ll create a frequency hash in Ruby called counts to store our counts.

The Data

Here’s the local passwd file used for this exercise:

The Output

The output given the report given the data from above would have these counts:

Shell Summary Report:
==================================================
Shell # of Users
----------------- ------------
/bin/bash 3 users
/bin/false 7 users
/bin/sync 1 users
/usr/sbin/nologin 17 users

The Code

Below is sample code to help you get started, which displays sorted output in a formatted text table.

Code Sample with Report Output

Some interesting notes on Ruby, you can use the C-language printf function and string repetition operator with * for formatted output.

In Ruby, you can iterate through not only lists, but also hashes:

list.each { |item| puts item }
hash.each { |key,value| puts "#{key}:#{value}" }

You can also sort a hash by its keys with hash.sort().

Conclusion

This article merely presents the problem, and follow-up articles (two parts), I’ll show how to build the hash dynamically line-by-line using conditional loops and then demonstrate later a more functional style of programming with map() and select() methods.

Here are the takeaways thus far:

  • creating an empty hash, e.g. newhash = {}
  • formatted output with printf
  • repetition operator *
  • enumerating and sorting a hash

--

--

Joaquín Menchaca (智裕)
Joaquín Menchaca (智裕)

Written by Joaquín Menchaca (智裕)

DevOps/SRE/PlatformEng — k8s, o11y, vault, terraform, ansible

No responses yet