I’ve wanted to create a web app similar to Code Wars for some time now. Several tutorials have quizzes built into their lessons, but they never do them completely well and they only supplement the lesson. I think a web app (and a mobile app) would be a good way for learners to stay sharp when they don’t have the focus or time for proper programming. I’d also like to make the thing open source similar to the Odin Project.

I don’t want to tarry long, so here’s an example quiz problem.

The Reduce Method Alias

Some Ruby methods have aliases (for example, you can call #length or #size on an array; these two methods do the same thing) The #reduce method can sum an array of numbers, like so [5, 6, 7, 8, 9, 10].reduce(:+) # => 45. Which of the following is the only alias of the #reduce method?

  • #inspect
  • #inject
  • #group_by
  • #select
  • #sum

The user would then select their answer, and either click a button to check their answer or the check would be revealed upon selection. Probably the latter for a better user experience. But it wouldn’t be just a boolean “Correct” or “Incorrect”. Each choice would have an explanation as to why it was right or wrong; true or false.

You’ll notice I made all the multiple choice options reasonably feasible answers. Having a choice like #foobarbaz wouldn’t be incredibly constructive. If #inspect is selected (which is alphabetically near to the correct answer), the user would see that it’s incorrect (some CSS animation showing a red X) and a message as to why it was incorrect, like “The #inspect method is used instead of #to_s to get a more detailed description of the object in question. Read more at railsmaster.net.”

The remaining options would look as follows when selected:

  • #inject

    Correct! The #inject method is the same as #reduce. Read more here for some history of the alias..

  • #group_by

    Incorrect. #group_by iterates over an array and returns a hash where the key/value pairs are the grouping condition and matching elements, respectively.

  • #select

    Incorrect. #select is used to return a new array of elements that match some condition.

  • #sum

    Incorrect. Although the example of summing values in an array was used, Ruby does not have a #sum method built in, and #reduce can do more than add values together. For example, we could have multiplied all the array elements: [5, 6, 7, 8, 9, 10].reduce(:*) # => 151200

Going through that exercise was honestly a bit grueling. At first glance, the methods seemed close enough to warrant differentiation. But the process of providing meaningful explanations was a bit tough to contrast. Five choices is probably the max I’d want in choices, and if the question isn’t asking for “True” or “False”, a minimum of three choices would be good. I’d still like to see explanations to provide rationale for why an incorrect choice was supplied.

So that’s the gist of what I’d like to build. Time to create a new repo for this project. https://github.com/chemturion/rubystem