3 Charming Ways to Crash Your Computer

I love infinite loops because they’re scary the way infinity is scary. And, like us, computers are awkward about infinity: when faced with too much at once, they become unresponsive. Perhaps you aren’t as awkward about infinity, but when I’m faced with too much, I, too, ‘become unresponsive’ for a bit, before I restart my infinite idle loop and consider the next mundane detail of my life.

But infinite loops aren’t always scary. In fact, they’re used for both good and mischief — although when they are used for good, they’re often called daemons! Daemons, operating systems, video games, and Web Sockets are some extremely common examples of useful infinite loops, which continuously ‘listen’ for changes or user input.

Because I am mischievous at heart, I’d like to share a few charming ways to crash your computer with infinite loops, and what I learned while writing them!

1. Being Recursively Annoying

def do_you_liek_pina_colada?
puts 'do you liek pina colada?'
gets
puts 'and walking in the rain?'
gets
do_you_liek_pina_colada?
end

This Ruby method is just as annoying as having a certain song stuck in your head all day. It’s even more annoying when you remove both gets lines! But what’s interesting about it is that it doesn’t need to be in loop form to be a loop — all it has to do is reference itself.

Recursive methods are particularly useful in games.

Here’s another recursive script penned by one of my Flatiron School classmates:

def ece
ece while ece
end

2. Being Recursively Obstinate

Sometimes, recursive methods / functions do the opposite of what you think. Instead of running forever — they don’t run at all! So here’s a little something I wrote based off of an old favorite:

> def buffalo(buffalo)
> buffalo(buffalo)
> end
=> :buffalo

Note the return value, because we’re going to care about it later! But right now, I’m going to try to run this:

> buffalo(buffalo)
ArgumentError: wrong number of arguments (given 0, expected 1)

Oh, not enough arguments? Let’s give you some more!

> buffalo(buffalo(buffalo(buffalo)))
ArgumentError: wrong number of arguments (given 0, expected 1)

Poor dear. You must be hungry! Have some more!!!

> buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo(buffalo))))))))))))))))))))))))))))))))))))))))
ArgumentError: wrong number of arguments (given 0, expected 1)

Still not enough arguments!! Now, at this point in time, I started to wonder why the return value of any Ruby method definition is a symbol, or, in this case, :buffalo. The answer is that someone thought it’d be a good idea to standardize this behavior in Ruby 2.1, because the returned symbol can be used for methods and macros that take in entire methods as arguments. If you’re a Ruby developer, you’ve probably used some:

private
def buffalo(buffalo)
buffalo(buffalo)
end
private :moose
class Buffalo
attr_accessor
def buffalo
buffalo
end
end
class Buffalo
attr_reader :moose
end
alias_method :moose, :buffalo

Having learned this about symbol return values (read more about symbols here), I made the obvious choice:

> def buffalo(buffalo)
> buffalo(buffalo)
> end
=> :buffalo
> buffalo(:buffalo)
SystemStackError: stack level too deep
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
... 10873 levels...
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):36:in `buffalo'
from (irb):38

BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO BUFFALO!

3. While True is True…

I can’t believe I couldn’t find an already-existing valentine with this on it, so I drew one out real quick here:

just in time for Halloween!

Whether you’re using while(true === true), while 1==1, while kittens, or while 0 < 100 this is one of the easiest-to-make infinite loops! I like them so much, I made some in Ruby, Javascript, and Bash…

Note: Always save your work before crashing your laptop! Most of the the following scripts will crash a slow computer or freeze your browser, so if you want to run any, make sure you read through and understand them first!

Javascript

function me(){
do {alert("I love you")}
while (you())
}
function you(){
do{alert("I love you too")}
while (me())
}
maximum call stack size exceeded!
function why(){
var why = 'why '
do{alert(why)
why += why}
while(why)
}
// other js infinite loops...
while (true === true){
console.log(“I love you”)
}
const theWorld = document.getElementsByTagName('body')[0]
const love = "love"
var alpha = 0
var omega = 100
function eternal(suffering, damnation){
return damnation.appendChild(document.createTextNode(suffering))
}
while(alpha < omega){
let love = "destruction"
eternal(love, theWorld)
}

Bash

feel lonely often? write a cron that says it loves you every 60 seconds!

Ruby

I apologize that Javascript had the most examples: This week, I’ve been trying to practice my JS and React skills! So if you’d like to share some of your favorite infinite loops, please get in touch: I want to know them!

This post marks the beginning of my very last week at Flatiron School. It’s been an unusual 3 months, during which I learned an incredible amount, and was fortunate enough to meet some of the most committed instructors and passionate students I’ve ever met. It’s impossible to express how good of a decision it was to enroll, and how glad I am to have been a part of this cohort. Thanks so much, guys.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.