Posts

Showing posts from December, 2018

Would you like an extra end with that? Curly Braces vs. Do End Blocks

Image
As a new programmer, I developed (get it?) a quick growing affinity towards using curly braces {} in my code, because I just simply find it cleaner to read, and when it comes to coding, I am Mr. Clean. Plus, who really likes seeing 10 ends in a block of code? And if they both function the same way, why not use the one that's more pleasing to the eye? Well, actually, they are in fact not the same, as I have learned. Here's how it works: Let's say we want to iterate through an array of numbers [1, 2, 3, 4, 5, 6, 7] and return a new array of numbers divisible by 3, we could write the method out in two ways. First, the curly braces (clean) way: And then the do..end (ugly) way: Again, both yield the same result here, but there is a difference. With the curly braces, the block in the braces are passed to the rightmost method, while with the do end block, the code within the block is passed to the leftmost method. You'll see that given the following example, th