Ruby Kata Two (First Approach)

The coding kata is a way to get programmers practicing their craft just like musicians and martial artists do by: “applying the theory over and over again, using feedback to get better every time“. You can read more about coding katas here.

The binary chop, the second kata in the series, tasks the programmer to implement a binary search routine using 5 different methods. A binary search is a way to find the position of a value (key) within a sorted array of values.

Example, given an array_values = [1, 2, 4, 6, 8] with a key = 6, the method should return 3 when the array_values and key variables are passed in as arguments of the method, if the key can’t be found, the method should return -1. A quick implementation in ruby can be seen below.

This is one of the sweetness of ruby, however this defeats the purpose of the kata, which is to make the programmer implement the solution. Below is my first solution to Kata 2, I will appreciate your feedback.

Here are the test cases which all pass.