How Minions taught me Ruby

Jordan Poulton
Sep 25, 2019 · 5 min read
Many smart. Very help. So wisdom.

When learning to code Ruby, a mantra you hear oft repeated is…

EVERYTHING IN RUBY IS AN OBJECT

And also that every object has 2 key elements:

State — the specific settings of the object

Methods — that allow those objects to be manipulated

Now, personally, I really struggled with this statement ‘everything in Ruby is an object’ — a lot of people do. I kept asking “but what actually IS an object?!?”, and the response — ‘the fundamental unit in object oriented languages’, didn’t feel particularly satisfactory.

So, permit me an indulgence a little, as I describe objects in terms of ‘Minions’. (You don’t need to know the movie… You just need to know that they’re simple, dumb little alien dudes!)

An basic object… Kinda
An basic object… Kinda

If you imagine an object to be a minion, then your role as a programmer would be simply to build armies of minions to do your bidding. Nothing more.

I’m going to stretch this metaphor throughout this essay, but hopefully it will help you understand objects like it helped me. For the purposes of this session, I will be using ‘minion’ and ‘object’ interchangeably.

One great thing about Ruby (and with all programming languages), is that the creators of the language created a bunch of inbuilt functionality for you. Some actions are so common, that they come with the language by default, saving you the effort of having to build it yourself.

‘Number’ is a type of Minion…

For example, there’s a ‘class’ or ‘type’ of minion, as we’ve already seen, called Numbers.

These come with a bunch of abilities, or powers, that you can make use of, and these are accessed via something called ‘dot notation’.

For example, one of the inbuilt abilities of Numbers is to assess whether they’re odd or even, like so:

p 2.even?

You’ll see this returns true… Because .even? is something that numbers respond to. (Please note, the ‘?’ here is seen by the interpreter as just another string character, nothing special).

‘String’ is another class or ‘type’ of Minion too

Another class of minion, (programmers would call this another ‘data type’) is String. These, as with all minions, come with their own set of inbuilt abilities, that you too can access using the ‘dot notation’ — what do you think the following expressions return?

p “Jordan”.reverse

As you’ll see, the Ruby developers know that things like even? for Numbers, and things like reverse and length and downcase for Strings, are so common, they should come as standard in the Ruby language.

But these particular inbuilt methods are a little boring… You just ‘call’ them (using the dot notation), and they return something. It gets a little more interesting when you discover that there are more exotic types of abilities, where you — the programmer — give some particular information, and the minion uses that to do some more interesting stuff… This is technically called passing an argument to a method.

Don’t worry about memorising the syntax here — just try to get a metaperspective on how arguments are used in conjunction with methods:

“Jordan”.end_with?(“n”)

This should be intuitive, but let’s be really clear about what’s going on. We have a string, “Jordan”, and we’ve got 2 expressions — the first, where we’re calling its end_with? method, and the second, where we’re calling its start_with? method.

Do you see what comes after it? This is the bit where we pass our ‘argument’ to the method. I like to imagine that, for both these methods, it’s like the minion walks around with her hand out, and you need to *give* her something for this specific ability to be invoked.

Imagine if it were a person you were talking to, asking about their name — and you ask the question — “Does it start with…”

That can’t be the end of the sentence, right? The person would almost certainly ask “Does it start with WHAT?!”

Well, passing an argument allows you to specify that ‘what’. In the first example, as you would imagine, since “Jordan” DOES indeed end with the letter “n”, when we call end_with? on the String “Jordan” and pass the letter “n” to it, it returns true.

In the same way, “Jordan” starts with a CAPITAL J and not a lower case j, so when we call start_with? on the String “Jordan” and pass it the argument of “j”, it returns FALSE, because “J” is different to “j”.

You should also note that some minion’s abilities (in technical terms, some objects’ methods) require more than one argument… It’s like they’ve got 2, or sometimes many hands held out, and you need to put something in those hands to make it work. For example:

name = “Jordan”

I highly recommend executing this code locally, don’t just read (and NEVER copy-paste)!

Hopefully you’ll see now that the variable name returns the string:

J@rd4n

Can you tell why?

This is because the sub method takes *2 arguments*, the first being the letter to be substituted, and the second being the letter you want to sub in.

What then. would the following code do?

name.sub(’n’, ‘n Poulton’)

Admittedly, it’s a funny way to do it, but hopefully you can see that it should now return

J@rd4n Poulton

And that, my friends, is how minions, their abilities, and their cute little outstretched hands, map to objects, methods, and arguments required by those methods.

Now go out and practise using this mental model — there are some great exercises on Code Wars — just make sure you set it to Ruby and start with the ‘8 Kyu’ challenges!

Until next time :)

Jordan Poulton

Written by

Startups, Tech, Science, general geekery, occasional rants and random flights of fancy. I’m ex-@makersacademy and now Founder/CEO of FutureBuilders.co :)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade