Ruby is a dynamic, reflective, object-oriented, general-purpose, open-source programming language. RubyLearning.com is a thorough collection of Ruby Study Notes for those who are new to the Ruby programming language and in search of a solid introduction to Ruby's concepts and constructs. Speed up your Ruby programming learning process by joining 1000s of other would-be Ruby developers around the globe at the Online RubyLearning Class.
One way to learn new Ruby tricks, patterns & methods is to read code others have written.
But where do you find interesting code worth reading?
We have many open source projects, and coding challenge sites that allow you to see the solutions from other developers once you submit your own.
In this article:
I compiled 7 examples that I think you’ll enjoy.
I’m also going to give you some commentary on each example to help you get the most out of this.
Sum Of Two Numbers
In this example we want to find out if given an array of unique numbers, there is a combination of two numbers which adds up to a target number.
Code:
This is interesting because I’m using the product
method here.
When you use this method is like having a loop inside a loop that combines all values in array A with all values in array B.
Counting, Mapping & Finding
Let’s say that you want to find the missing number in an arithmetic sequence, like (2,4,6,10)
.
We can use a strategy where we calculate the difference between the numbers.
Our goal here is to find out what the sequence is.
Is it increasing or decreasing?
By how much?
This code reveals the sequence:
Once we know the sequence we can compare all the numbers to find the missing one.
Here’s the code:
Count how many Ruby methods we’re using here to do the hard work for us 🙂
Regular Expression Example
If you are working with strings & you want to find patterns then regular expressions are your friend.
They can be a bit tricky to get right, but practice makes mastery!
Now:
Let’s say that we want to find out if a given string follows a pattern of VOWEL to NON-VOWEL characters.
Like this:
Then we can use a regular expression, along with the match?
method to figure this out.
Here’s the code example:
Notice a few things:
- We use the
cycle
method so we can keep switching between the VOWEL regex & the NON-VOWEL regex. - We convert the string into an array of characters with
chars
so that we can use theall?
method.
Recursion & Stack Example
Recursion is when a method calls itself multiple times as a way to make progress towards a solution.
Many interesting problems can be solved with recursion.
But because recursion has its limits, you can use a stack data structure instead.
Now:
Let’s look at an example where we want to find out the “Power Set” of a given array. The Power Set is a set of all the subsets that can be created from the array.
Here’s an example with recursion:
Here’s the same problem solved using a stack:
The idea here is that on each pass of the algorithm we are either taking a number or not taking a number.
We branch out & try both outcomes so we can produce all the possible combinations.
Imagine a tree where each leaf is one of the solutions.
A few things to notice:
- The recursion solution is shorter
- The actual 'making progress' part of the algorithm (index + 1) is almost the same
- The stack we're using is just an array because there isn't a
Stack
class in Ruby
Method Chaining Example
This is my favorite example because it shows how powerful Ruby is.
Combining methods allows you to take the output produced by one method & pass it into another.
Just like a factory production line!
You start with some raw materials (input), then through the process of calling these methods, you slowly transform the raw materials into the desired result.
Here's an example:
Given a string, this code will find the longest repeated character.
Note:
- How this code is formatted to maximize readability
- Use of the
Symbol#to_proc
pattern (&:size
)
Btw, don't confuse this with the 'Law of Demeter'.
That 'law' is about reaching out into the internals of another object.
Here we are only transforming objects.
With Index Example
Would you like to have the current index while iterating over a collection of items?
You can use the with_index
method.
Here's an example:
Notice:
Ruby Programming Language Tutorial
- We combine
with_index
&even?
to find if we have to reverse the current word - Gsub without a block returns an
Enumerator
object, which allows you to chain it with other methods
Each With Object Example
Another interesting method is each_with_object
, and its friend with_object
.
You can use these two methods when you need an object to hold the results.
Here's an example:
In this example, we want to delete the last character when we find a #
symbol.
Notice:
each_with_object
takes an argument, which is the object we want to start with. This argument becomes the 2nd block parameter.- We are converting the string into an array of characters (
char
) & then back to a string when we are done (join
). - We are using a ternary operator to decide what to do, this makes the code shorter.
Ruby Programming Language Release Date
Ruby Programming Language
Summary
In this article, you have seen some interesting code examples with my commentary & explanations to help you write better Ruby code.
Don't forget to share this article so more people can enjoy it.
Ruby Programming Language Advantages
Thanks for reading!