Advanced Ruby Methods Continued

            Like I discussed last week ruby has many useful built in functions, I will cover more of them in this blog post and show examples of how they are used.

<=>

            Lets first talk about the sort operator this is a very useful yet simple operator to use. The sort operator simply returns one of three things -1, 0 , 1, this lets you know if an object, array, or variable is less than, equal, or greater than. For an array, the sort operator compares each element to each other at the same index.

            [ “a”, “a”, “c” ]    <=> [ “a”, “b”, “c” ]   #=> -1

            This return negative one, as the ‘b’ is greater than ‘a’ (side not letters have values derived from ascii or Unicode tables) therefore the sort function returns negative one since the first array is smaller.

[ 1, 2, 3, 4, 5, 6 ] <=> [ 1, 2 ]            #=> +1

[ 1, 2 ]             <=> [ 1, :two ]         #=> nil

These next two examples show that if an array is longer it will be considered larger as it will be being compared to nothing. The second example shows that there are edge cases as it does not know how to compare two elements like the key in the second array so it returns nil.

bsearch

            The second method I will be talking about is bsearch or short for binary search. Which I am sure most have heard of binary search but in short it is simple a search that will find all the values in an array which meets a given condition in O(log n) time which is an extremely useful and efficient search algorithm.

ary = [0, 4, 7, 10, 12]

ary.bsearch {|x| x >=   4 } #=> 4

For this example, it simply finds the first element that is equal or larger than 4 it does not find all elements that are larger than 4.

ary.bsearch {|x| x >= 100 } #=> nil

You can also see it return nil if the condition is not met in the array.

fill

            Another useful yet simple method which allows you to  transform or overwrite an array in whatever way you would like.  If you have an array of a given size and want square all the numbers or fill it with completely different values or fill just part of the array fill allows you to do that.

a = [ “a”, “b”, “c”, “d” ]

a.fill(“x”)              #=> [“x”, “x”, “x”, “x”]

a.fill(“z”, 2, 2)        #=> [“x”, “x”, “z”, “z”]

a.fill(“y”, 0..1)        #=> [“y”, “y”, “z”, “z”]

a.fill {|i| i*i}         #=> [0, 1, 4, 9]

a.fill(-2) {|i| i*i*i}   #=> [0, 1, 8, 27]

As you can see the first example filled the whole array with ‘x’s then filled the last two spots with ‘z’s, by writing the fill element then the fill starting position then the number of fill elements. The next one fill the first two indexes with ‘y’s by instead of specifying the number of fill elements just specifying the index bounds to fill all them with. The next two use absolute indexes as a fill element squaring and cubing them.

Hopefully, these methods help you happy coding!

Source:

  1. https://ruby-doc.org/core-2.7.0/Array.html#method-i-fill

Leave a comment

Design a site like this with WordPress.com
Get started