Advanced Ruby Loops

            While we’ve most likely all heard of the traditional “while” and “for” loops that most modern programming languages use. Many languages have a wide variety of loops, iterators and functions that behave like loops that are used to resolve a wide array programming problems in an efficient manner. For this blog we will be talking about loops and functions that can be used on ruby arrays, although many languages have may have similar processes the  fact that ruby is a dynamic open source languages means that it by far has the most of any main stream language. Besides that the main goal of this blog post is to get you wondering if there are built in functions or loops that you can look up and start using now,  that will allow you to save time programming, make your code more readable and more efficient. Regardless of the programming language you are currently using.

Select

            The first enumerator (class that allows both internal and external iterations) is select is an extremely useful function. I should be used when you are trying to filter down an array to the values that meet a certain condition. If you are familiar with SQL it works very similarly. If given the array [1, 2, 3, 4, 5] and you want to select all the even numbers you would simply write.

 [1,2,3,4,5].select {|num| num.even? }     #=> [2, 4]

This is obviously an extremely simple example but select is an extremely simple yet useful enumerator.

Map

            The next enumerator is to be used when you would like to transform all the elements of the incoming data structure uniformly. Say you want to want to find the square of all the numbers in the previous array you would write something like this.

[1,2,3,4,5].map {|num| num * num }     #=> [1, 4, 9, 16, 25]

Or a common use is to convert hash values to symbols, this is extremely useful as data can common in many shapes and sizes when you are using a open source API and its always best to convert your data so it is consistent across your applications. You could do this with this simple map function and the to_h function.

hash = { bacon: "protein", apple: "fruit" }
hash.map { |k,v| [k, v.to_sym] }.to_h
# {:bacon=>:protein, :apple=>:fruit}

Flatten

            This is an extremely straight forward ruby function; its man uses are to extract nested data from a structure. It is very convenient to use as it can be tiresome to write your own function that does the same thing. Flatten simply takes an array inside of an array for example and “flattens” it to a single array, for instance if you have the array t = [ 4, 5, 6, [7, 8] ] 

t = [ 4, 5, 6, [7, 8] ] hash.map { |k,v| [k, v.to_sym] }.to_h
t.flatten(1)       #=> = [ 4, 5, 6, 7, 8 ]

You probable noticed the number in the parenthesis this indicates the number of levels you would like to flatten the array by for instance if you had the array t = [ 4, 5, [6, [7, 8]] ]  

t.flatten(1)       #=> [ 4, 5, 6, [7, 8] ] 
t.flatten(2)       #=> [ 4, 5, 6, 7, 8 ]

Overall, most of these functions/loops/enumerators are pretty straight forward yet extremely useful. So, look them up for yourself I’m sure you will find many uses for them!

Sources:

  1. https://ruby-doc.org/core-2.7.0/
  2. https://www.rubyguides.com/

Leave a comment

Design a site like this with WordPress.com
Get started