Methods to the Madness - My First 10 Ruby Challenges
Jonathan-Bryant19
Posted on March 10, 2022
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 and codewars 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.
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
Documentation
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
Documentation
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
Documentation
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
Documentation
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
Documentation
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
Documentation
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
Documentation
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
Posted on March 10, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024