To get a sample of 5 elements from an array in Ruby, you can use the Array#sample
method. The sample
method returns a random element (or elements) from the array.
To get a sample of 5 elements, you can pass the number 5 as an argument to the sample
method:
main.rb97 chars4 lines
This will output something like:
main.rb16 chars2 lines
Note that the sample
method does not modify the original array, it just returns a new array with the sampled elements.
gistlibby LogSnag