Art of the “.sample”

Jabair
3 min readMar 31, 2021

I first came across the .sample method during Phase 1 of my time as a software engineering student at the Flatiron School. Previously, I had seen this method called in classroom examples and the ruby documentation, but was still unsure on how I would use this in my everyday coding experience. Fast-forward about a week and a half and there I was searching for a way to return a “randomized” piece of information from an array to build a feature in my first CLI application.

The goal of the CLI application was to return a fortune to a particular user from an array of seeded data. The data in this case were strings of short quotes, sayings, or song lyrics. As a beginner this baffled me quite a bit and it took up until the 25th hour for me to actually come across the .sample method. Let’s break it down.

From the ruby documentation, the sample method will, “Choose a random element or n random elements from the array.” What this is saying, is we have two ways to use our ‘.sample’ method the first would look like this.

Let’s say our array is a series of numbers:

array = [1, 2, 3, 4, 5, 6, 7, 8, 9]

array.sample could then output #=> 4

Your result will vary, precisely because this is a method designed to produce a randomized action. The ruby documentation also states, “The elements are chosen by using random and unique indices into the array in order to ensure that an element doesn’t repeat itself unless the array already contained duplicate elements.” Meaning, our next output could very well be any of the numbers from that array, which was our original intention.

The .sample method can also be called with an argument. The argument in this case specifies how much of something we would like to return. Let’s look at the same array from above.

array = [1, 2, 3, 4, 5, 6, 7, 8, 9]

array.sample(2) => [4, 8]

array.sample(2) =>[3, 9]

It’s important to note that our argument is not pointing to a specific instance of a number, but rather the amount of what we would want to return. While building this CLI application, outputting a random message was really our only goal, but as you move further into working with seed data in particularly, you will find yourself using the .sample method to populate your database in development.

Let’s say for example we wanted to create 20 instances of a Brewery in our database. Some information we might need would be a name, city, and a style. We filled in our name and city attributes using a Faker Gem, but we still have to deal with our style attribute. Since we know .sample will randomly select items from our array, our next step would be to create that array in our database. In this example we do this with the brewery-type array.

brewery_type array

Next let’s call .sample on the array to populate the style attribute.

style: brewery_type.sample

This will successfully populate each instance of a Brewery’s style attribute with one of the options from our brewery_type array. Ultimately, the .sample method will save you the headache of writing copious amounts of repetitive code during initial development.

Running rails db:seed at this point should run without any errors, but if you were to check your data. You could do so in the console with .all. This will show you every instance of the Brewery class and when looking at each instances style attribute you will now have a variety of style attributes based on our brewery_type array.

--

--