Hey everyone,
while working through the Ruby exercises for the 12in23, I’ve stumbled upon a topic that I find not enough emphasized in the learning track: blocks and yield. Up to now I’ve needed it twice for the solutions of some exercises; once in the list-ops exercise:
def mapper(array, &block)
result = []
while not array.empty?
result.push(yield(array.shift))
end
result
end
And once in the binary search tree exercise, where part of the task is to implement the Bst#each
method. I’m not sure if there are other exercises that also might profit from it (I’m just 50% through with the track). It’s of course easily searchable (at least if you know what you need to be looking for), but I still feel this should be a concept to learn in the learning path, as it is something essential to Ruby and something you don’t find in many other (or at least classic) programming languages?
Me myself, I found this page a really helpful resource on the topic. This also touches procs and lambdas, which I think could also be interesting for the Ruby learning track, but at least (up to now, that is) I didn’t need these concepts in the exercises, so I feel it’s not as necessary as a learning concept as blocks and yield are.
I’m interested in what you think, and as always: Thanks for all the great work you guys are doing with the site! All in all the Ruby learning track is really great and I am having a lot of fun with it as well as with the exercises! Thanks to everyone who participated in it!