Methods to the Madness - My First 10 Ruby Challenges
Published: 2022-03-10 04:38:29

There's no better way to learn a new programming language than just writing code. Having just transitioned from JavaScript to Ruby for the first time, I found myself struggling to find the right method for the job. 

Websites like [LeetCode](https://leetcode.com/) and [codewars](https://www.codewars.com) are great for grinding out practice, so I decided to start logging which methods I needed to use and how often. To my surprise, I didn't wind up using the same method more than once across the first 10 Ruby problems I came across on [codewars](https://www.codewars.com). 

Some of these answers I came up with on my own while several others were listed under "Best Practices". Here's my running list of common Ruby methods. I'll update the list as I continue to complete problems in hopes of curating a list of some of the most common and fundamental Ruby methods.

## `delete`
[Problem for Context](https://www.codewars.com/kata/52fba66badcd10859f00097e)
[Documentation](https://ruby-doc.org/core-2.4.1/Array.html#method-i-delete)

The `delete` method takes an object as an argument and deletes everything from `self` that is equal to the object. I used the `delete` method below to remove all vowels from a given string:

```
def disemvowel(str)
  str.delete "aeiouAEIOU"
end
```

## `slice!`
[Problem for Context](https://www.codewars.com/kata/56bc28ad5bdaeb48760009b0)
[Documentation](https://ruby-doc.org/core-2.4.1/Array.html#method-i-slice-21)

The `slice!` method differs from `slice` in an important way. `slice` returns the element at a specified index while `slice!` deletes the element at the specified index and returns the deleted object. I used `slice!` to remove the first and last characters in a string:

```
def remove_char(s)
  s.slice!(0)
  s.slice!(-1)
  s
end
```

## `even?` and `odd?`
[Problem for Context](https://www.codewars.com/kata/53da3dbb4a5168369a0000fe)
[Documentation](https://apidock.com/ruby/Integer/even%3F)

These are extremely simple methods that can be called directly on integers (since everything in Ruby is an object). I'm used to using `% 2` in JavaScript to determine if a number is even or odd, but Ruby simplifies that process immensely. I used `.even` in a ternary to solve this problem:

```
def even_or_odd(number)
  number.even? ? "Even" : "Odd"
end
```

## `method`
[Problem for Context](https://www.codewars.com/kata/57356c55867b9b7a60000bd7)
[Documentation](https://ruby-doc.org/core-2.5.3/Method.html)

This is one that I have to keep reading the documentation on. As I understand it, `method` can be called on an object and, in doing so, creates an object that can be used to invoke a method elsewhere. Here's an example provided by the documentation:

```
class Thing
  def square(n)
    n*n
  end
end
thing = Thing.new
meth  = thing.method(:square)

meth.call(9)                 #=> 81
[ 1, 2, 3 ].collect(&meth)   #=> [1, 4, 9]
```

In this example, calling `method` on `thing` allows the `square` method to be called later on. I used `method` to build a basic calculator:

```
def basic_op(operator, value1, value2)
  value1.method(operator).(value2)
end
```

## `split`
[Problem for Context](https://www.codewars.com/kata/57cebe1dc6fdc20c57000ac9)
[Documentation](https://ruby-doc.org/core-2.4.0/String.html#method-i-split)

`split` is a handy method to keep in your back pocket if you ever need to iterate over a string. `split` takes a delimiter as an argument and separates a string into substrings based on that delimiter. I used `split` to break apart a sentence string into individual words in order to determine which word was shortest:

```
def find_short(s)
  lengths = []
  s.split(" ").each do |w|
    lengths.push(w.length)
  end
  return lengths.min
end
```

## `min` and `max`
[Problem for Context](https://www.codewars.com/kata/577a98a6ae28071780000989)
[Documentation](https://ruby-doc.org/core-2.4.0/Range.html#method-i-max)

`min` and `max` are simple range methods that return the maximum or minimum value in a range, respectively. They're exceptionally easy to use and much more straightforward than their JavaScript counterparts. I used `max` and `min` to write a few simple range functions:

```
def min(list)
  list.min
end

def max(list)
  list.max
end
```


## `to_i`
[Problem for Context](https://www.codewars.com/kata/557cd6882bfa3c8a9f0000c1)
[Documentation](https://ruby-doc.org/core-2.4.0/Range.html#method-i-max)

The `to_i` will convert a string to an integer when called on a string. Again, calling the conversion method directly on the object itself makes running the conversation very simple. I used `to_i` to parse an integer from a string:

```
def get_age(age)
  age[0].to_i
end
```