Murder Mystery Night with Arrays

Welcome to a fun evening of deceit and murder at Hill House.

giphy
#pop

Like a revolver in a dining room, this pops off the last item in the array and shows you the body.

victims = ["Yvette", "Telegram Girl", "Officer", "Stranded Motorist", "Mrs. Ho", "Mr. Boddy"]

victims.pop
=> "Mr. Boddy"

victims
=> ["Yvette", "Telegram Girl", "Officer", "Stranded Motorist", "Mrs. Ho"]

Give it a number and it will do a multi-hit job and take out the last n elements. If there’s fewer than the number you gave, then you can rest in your dissatisfaction without getting a nil back.

victims = ["Yvette", "Telegram Girl", "Officer", "Stranded Motorist", "Mrs. Ho"]

victims.pop(3)
=> ["Officer", "Stranded Motorist", "Mrs. Ho"]

victims
=> ["Yvette", "Telegram Girl"]

victims.pop(4)
=> ["Yvette", "Telegram Girl"]

victims
=> []

#shift

To remove an element from the beginning instead of the end, put down the revolver and locate a shiv, we’re getting shifty. Same argument rules apply for removing n elements.

victims = ["Yvette", "Telegram Girl", "Officer", "Stranded Motorist", "Mrs. Ho", "Mr. Boddy"]

victims.shift
=> "Yvette"

victims
=> ["Telegram Girl", "Officer", "Stranded Motorist", "Mrs. Ho", "Mr. Boddy"]

victims.shift(3)
=> ["Telegram Girl", "Officer", "Stranded Motorist"]

victims
=> ["Mrs. Ho", "Mr. Boddy"]

victims.shift(5)
=> ["Mrs. Ho", "Mr. Boddy"]

victims
=> []

#delete_at

If you care more about murdering anyone in the Conservatory than targeting any one person, then use #delete_at to specify the place at which you would like to remove an element.

victims = ["Yvette", "Telegram Girl", "Officer", "Stranded Motorist", "Mrs. Ho", "Mr. Boddy"]

victims.delete_at(3)
=> "Stranded Motorist"

victims
=> ["Yvette", "Telegram Girl", "Officer", "Mrs. Ho", "Mr. Boddy"]

Of course, if there isn’t a Conservatory to begin with, then you’re going to get yelled at.

victims.delete_at(7)
=> nil

#delete

Then again, if someone is out to blackmail you, it’s probably best to simply target that one person. Forget the revolver or the knife, consider #delete a targeted missile that could blow up on you if you enter in something that isn’t there.

victims = ["Yvette", "Telegram Girl", "Officer", "Stranded Motorist", "Mrs. Ho", "Mr. Boddy"]

victims.delete("Yvette")
=> "Yvette"

victims.delete("Wadsworth")
=> nil

Of course, we set up a message to be sent in place of getting nil.

victims = ["Yvette", "Telegram Girl", "Officer", "Stranded Motorist", "Mrs. Ho", "Mr. Boddy"]

victims.delete("Wadsworth") { "Frankly, Scarlet, I don't give a damn"}
=> "Frankly, Scarlet, I don't give a damn"

ipcbhc46b53r2

#compact

If you’ve found that your French maid, Yvette, has been murdered, you can use #compact to help clean all the nil out of your array. Your spotless array will be returned to you, even if there weren’t any nil items to begin with.

victims = [nil, "Telegram Girl", nil, "Officer", "Stranded Motorist", "Mrs. Ho", "Mr. Boddy"]

victims.compact
=> ["Telegram Girl", "Officer", "Stranded Motorist", "Mrs. Ho", "Mr. Boddy"]

Use the destructive version and you will, however, get a nil if there weren’t any spots to clean.

victims = ["Telegram Girl", "Officer", "Stranded Motorist", "Mrs. Ho", "Mr. Boddy"]

victims.compact!
=> nil

giphy1

#uniq

Let’s clarify the victim types and view the array without any duplicates (or modifying it) thanks to some help from #uniq.

victim_types = ["staff", "visitor", "visitor", "visitor", "staff", "host"]

victim_types.uniq
=> ["staff", "visitor", "host"]

victim_types
=> ["staff", "visitor", "visitor", "visitor", "staff", "host"]

By using the destructive version, we can permanently alter the array.

victim_types = ["staff", "visitor", "visitor", "visitor", "staff", "host"]

victim_types.uniq!
=> ["staff", "visitor", "host"]

victim_types
=> ["staff", "visitor", "host"]

We can make things even crazier by passing a block to specify what we want to compare. For example, let’s make an array of arrays where we have both the victim as well as their type…

victims_and_types = [["Yvette", "staff"], ["Telegram Girl", "visitor"],  ["Officer", "visitor"],  ["Stranded Motorist", "visitor"], ["Mrs. Ho", "staff"],  ["Mr. Boddy", "host"]]

If we compare whatever returns by calling #first on the inner arrays, we will compare the victim’s name/title. They’re all unique, so everyone is returned.

victims_and_types.uniq {|victim_detail| victim_detail.first}
=> [["Yvette", "staff"], ["Telegram Girl", "visitor"], ["Officer", "visitor"], ["Stranded Motorist", "visitor"], ["Mrs. Ho", "staff"], ["Mr. Boddy", "host"]]

If we compare using the second item (index 1), now we are using the type. The first occurrence of each type is returned.

victims_and_types.uniq {|victim_detail| victim_detail[1]}
=> [["Yvette", "staff"], ["Telegram Girl", "visitor"], ["Mr. Boddy", "host"]]

Again, we can permanently alter the array by adding a bang (!), but we will get a nil if there aren’t any duplicates.

victims_and_types.uniq! {|victim_detail| victim_detail[1]}
=> [["Yvette", "staff"], ["Telegram Girl", "visitor"], ["Mr. Boddy", "host"]]

victims_and_types
=> [["Yvette", "staff"], ["Telegram Girl", "visitor"], ["Mr. Boddy", "host"]]

victims_and_types.uniq! {|victim_detail| victim_detail[1]}
=> nil


Thanks to the 1985 film, Clue, for being a fun inspiration on breaking these down.

Leave a comment