BYOM

Yosef S Adelman
5 min readJun 2, 2021

(Bring Your Own Modules)

As a beginner in programming, one of the many mysteries is where built-in functions or methods come from. Depending on what language you’re learning, you may have heard of things like packages, imports, gems, and libraries — just to mention a few. These mysterious processes allow you to do things like:

  • math.pi (Python)
  • return(<h1>I’m not your regular ol’ HTML</h1> (JavaScript)
  • binding.pry (Ruby)

What is the mystery?

It can be extremely overwhelming to comprehend these abstract functionalities and where they come from, so let me give you an analogy:

Gosh, analogies are just, so great

You are a brick. You’ve spent your life living safely on the floor of a warehouse. One day, you get shipped out, hoisted up on a giant crane, and plopped on top of a tower, taller than anything you could have imagined.

Now I may not know much about bricks, but I’d sure be scared to be so high up. I just have to take it for granted that I won’t fall down? What exactly is below that’s holding me up so high? I’m just a brick! How should I know what’s holding me up? Am I going to have to hold up other bricks?

I’ve felt like a brick before while coding. I’ve followed the instructions; “ …Import this, require that, extend it here, and include it there… Great! Now you can run MakeAGame(), and you’ve made a game! Great job! :)”

Yeah, I don’t know what the hell I just did, and if something goes wrong, we are all just going to burn.

What’s actually happening

Although you and I may feel like we were just plopped on top of decades of advanced and complicated code, we weren’t. We were plopped on decades of basic and simple code. The years of evolution are still there, but it really isn’t that daunting.

It all started when one coder realized that they needed to run a similar task many times in different places… Time for an example!

Anon is a Ruby coder, and often has to pick the highest number in an array. Given the array of [54,7,94,15,46,7], Anon gets to work:

highest_number = 0
[54,7,94,15,46,7].each do |number|
if highest_number < number
highest_number = number
end
end
end
return(highest_number)

In the .each iterator, the code checks each number to see if it’s higher than the value saved in the highest_number variable (initialized to 0). If it’s higher, it will set highest_number to that new value. Otherwise it just ignores it, and moves on to the next element in the array. When the iteration is complete, it returns the highest number in the array. Mission completed! The problem is that Anon needs to use this logic multiple times.

You may be thinking…

Why not make it into a function? And you’d be right, that is exactly what Anon should do.

def get_highest_number(array)
highest_number = array[0]
array.each do |number|
if highest_number < number
highest_number = number
end
end
end
return(highest_number)
end

So Anon followed our advice, and wrapped it into a function called get_highest_number, which takes a single argument of the array, and instead of initializing the variable to 0, Anon decided to go with grabbing the first element. Now Anon can access this function from anywhere in the same file. But Anon is super nice, so he wraps his function up into a module, and makes the module publicly available so nobody else has to spend extra time on writing the same code. Anon then makes another function:

def get_lowest_number(array)
lowest_number = array[0]
array.each do |number|
if highest_number > number
lowest_number = number
end
end
end
return(lowest_number)
end

Another widely usable block of code! Anon adds it to his publicly available module, and now coders worldwide don’t have to do all that logic themselves. Python is a great way to demonstrate this. If we were to import a library-

import statistics

…we can then use a function-

statistics.mean([1,2,3,4,5])

…which would tell us that the mean for the given array is 3. Lets go over what we just did. We imported publicly available code, given to us as the module called statistics. In this module, amongst many others, there is a function that, given an array, will do all the logic necessary to find the mean.

This is how the decades of coding stacked on top of each other. One coder wrapped a block of code in a function, so that they could use it multiple times in one script, or page of code. They used exports, modules, libraries, and requirements to pass it around multiple scripts on one computer. Then they passed it around internationally as packages using public code registries like npm, pip, rubygems, and others.

With all this in mind, consider that while coding, you can make your own modules and libraries for any repetitive code that you are writing. If Anon can pass a block of code around the world, you and I can easily pass a block of code around on our own file system, resulting in the cleanest, uber-factored code possible. Let’s see how do to that in ruby:

touch my_module.rb

In our just-made file my_module.rb, lets write some code:

module MyModule
def say_hello
"Hello!"
end
end

Now we have declared a module called MyModule, which lives in the file my_module.rb. MyModule so far has only one method in it, say_hello.

In order to use the method say_hello elsewhere, we have to do two things. First we have to be able to access the file where the module lives, which in our case is my_module.rb.

require_relative "[Some Path that ends in]/my_module.rb"

Now that we have access to the file, we have to tell our current location (wherever we are trying to use the method say_hello) that there is a module to be used.

include MyModule

With those two steps done, we can call MyModule.sayhello:

=> "Hello!"

--

--

Yosef S Adelman

Hey, I’m a software engineering graduate from Flatiron School.