Picking a Random Element from an array in Ruby

Florence Njeri
Feb 16, 2022

--

To pick a random element, you will use the sample ruby method. So to pick a random pun from the array below:

random_puns = ["Where did Noah keep his bees? In the ark hives.","An untalented gymast walks into a bar","My grandma is on speed dial and now I call her instagram.","When he proposed to her, she found it very engaging.","If Apple made a car, would it have Windows?","I used to be a baby but I grew out of it."]

You will use the sample method:

random_puns.sample()

Reference this to learn more about the sample method.

--

--

Florence Njeri
Florence Njeri

Written by Florence Njeri

Software Engineering || Career || Life

No responses yet