Image for post
Image for post

Ops Scripting w. Ruby: Frequency

Tracking Frequency in Ruby: Part I

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.

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.

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
list.each { |item| puts item }
hash.each { |key,value| puts "#{key}:#{value}" }

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.

  • formatted output with printf
  • repetition operator *
  • enumerating and sorting a hash

Written by

Linux NinjaPants Automation Engineering Mutant — exploring DevOps, Kubernetes, CNI, IAC

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store