Arrayclass offers some of the most-used methods in the ruby ecosystem. A few months back we published a quiz, testing knowledge of ruby arrays and their associated methods. In this short post we review average performance against this quiz and identify which questions posed the biggest challenge.
A few months ago we published a ruby arrays quiz. This was a 10-question quiz, allowing you to self-test your knowledge of this fundamental component of the ruby language.
In this post we will use the responses collected to see how people fared on average, and to identify which question posed the biggest challenge.
Breakdown by score
At the time of writing the quiz has a total of 324 attempts registered, with an average score of just 29%. The distribution of scores is summarised in the barchart below:
The mean score across all responses was 29%, whilst the modal scoring bracket was 30-39. Responses reaching the higher scoring brackets were rare. Indeed, at the time of writing, if you scored over 70% you were in the top 5% and only three responses exceeded the 80% mark!
Breakdown by question
We can look at how users performed against the individual questions. The bar chart below summarizes the percentage of correct responses for each question, you can click on the individual bars to see a screenshot of the question in each case.
The Hardest Question
From the graph, question 6 immediately jumps out as the hardest question, with only
You can learn more about the specifics of these different methods from the offical Ruby docs for
The actual question posed was as follows:
The question proposed a number of ways to generate the string "12345". Some of these techniques will give us what we want and some will not.
Our challenge is to select one or more options that do the job. One of the key pieces of knowledge required to answer this question pertains to the
"once,twice,three times a lady".split(",") => ["once", "twice", "three times a lady"]
The method splits the string into an array, based on the delimiter supplied, in this case we have passed a comma. If we refer to the docs for the method, we see that when we don't specify a pattern to split on then it will, effectively, split on whitespace:
If pattern is nil, the value of $; is used. If $; is nil (which is the default), str is split on whitespace as if ' ' were specified.Splitting a string with no whitespace, e.g.
'12345', will just give an array with a single element, i.e.
['12345']. We will see that this understanding is crucial to understanding two of our proposed solutions below. Let's now consider each option in turn:
(1...5).map(&:to_s).join(''): The range
(1...5)has three dots which means it will not include 5, so this expression results in a string of "1234". If we had used the two-dot version of the range, i.e.
(1..5), then we would have obtained the desired result.
splitto the string
"54321"leads to a single-element array (splitting on whitespace by default). The
Array#reversewill have no effect on the single-element array and the output after
joinwill just be
"54321". So this is not what we want. Note, had we used
split('')in this example it would have produced the desired output.
reverseto a string will do exactly what we expect, so this is a valid answer.
12345.to_s.split.sum(''): Converting the number 12345 to a string, then running
sumis logically sound, but note that
splitwill generate a single-element array, as discussed above. So the
split.sum('')operations have not net impact on the string, and the result is
"12345". But this is what we want, so it is a valid answer..
We have presented a short analysis of the results registered against our recently published Ruby Array Quiz.
The most difficult question was identified to be question 6, which challenged our understanding of
String methods, when used in concert.
We looked at the correct response for this question, identifying a couple of learning points along the way. In particular, the default behaviour for
String#split when called
with no arguments is to split on whitespace.
I hope you found this short article useful. If you have any questions or feedback we would love to hear from you on the comments. If you enjoyed this content please consider subscribing to be kept updated when new content is published. Thanks for reading!