Majority of people I know of that are great with computers tend to be not so great at sports. In tender years of their youth they are usually picked up the last for all sorts of sports activities. I believe this actually helps their computer skills as they get to spend more time with computer. After all everybody likes doing what he is good at. I’ve noticed couple of distinct phases.
I am assuming you are intrigued about Ruby blocks. My assumption is that you are aware that they exist but unsure of what they are or how to use them. From my experience main obstacle in understanding them is introduction of multiple concepts all at once. You need to understand closures and procs to understand blocks. Also Ruby mix-up with lambda vs. proc debate is not helpful either. There are some great posts about the subject. For example Understanding Ruby Blocks Procs and Lambdas.
Nightmare at 20,000 Feet via Wikipedia
I am going to bluntly ignore the details and just give the 20,000 feet look at it.
Block in Ruby is just another type of variable.
I was always ashamed of not being able to bang out code without looking at the documentation. One example was file opening/handling in .NET 1.1. I remember not being able to do it even if my life would depend on it. Later on I’ve read that Microsoft made the study about APIs usability. One of the APIs that missed the mark was the IO.
The main complaint was that you need way too many lines of code for basic scenarios.The other complaint was that you need the knowledge of rather abstract (from the usage point of view) inheritance tree. That was a bit of relief. I stopped feeling as such an imposter that was getting payed for doing nothing. Still I regarded this as my flaw and was always irritated when unable to do simple stuff because I forgot API details.
Similarly, after finding out about Ruby I was amazed that alpha geeks figured it out five years before me. When I think about it more carefully, it would not be logical any other way. I speculate that they were able to position Ruby as a Smalltalk derivate that has some Perl syntax sugar and some Lisp functional goodness. That enabled them to label/recognize the whole technology relatively fast and easy.
Yesterday I’ve subscribed to Rubies in the Rough from James Edward Gray II and read his article “Doing it Wrong”.
In his article he questions (along with some other rules) the rule of never using regular expression for xml parsing.
As it turned out it was a fortunate move since six dollars and one day latter I came across .xml that needed to be parsed.
I have a confession to make: I’ve always hated xml parsers. This particular .xml did not even use xml strengths; data inside was all messed up:
If you don’t know Ruby please take a few moments and look at code below. You may like it.
As they say:
The beauty of Ruby is found in its balance between simplicity and power.
If after this your mouth is watering, you can try Ruby online.
I’ve been a TDD zealot for the last 7-8 years. I take pride in my code being well tested, my solutions being user friendly and my design being test driven.
When I test I know my system works. I sleep like a baby. I have a balls of brass. I can embrace change and laugh at how easy it is to add new requirements.
The thing is, in my life I’ve seen a fair amount of big systems that are terrible. They all have one thing in common. Everybody hates them. Programmers, users… but they do work, sort of. They are slow and buggy but nevertheless they deliver some value to the users.
Enterprise is euphemism for lame systems that barely work for 300 users?
Oddly, in many of those terrible systems funny thing happened. After a while guess what? People involved (users and programmers) made those terrible systems work (just enough) and all of a sudden you have a winner. Bigger the system, bigger the win. Bigger the mess, bigger the ecosystem of additional companies making it all work.
Think Windows & antivirus for example.
I wonder how smug IBM felt when developing OS/2 with all the cool stuff…and than a big nothing happened. Right now you might be thinking that I’ve lost it. You are ready to click away… but let see some examples:
It all starts with “A Little Unnecessary Smalltalk Envy” from Bob Hutchinson.
Quick warning: If you don’t like monkey patching… Run away now it is still not too late…
This enables you to stop treating nil as a special case:
A rather unfortunate series of events led me ending up in my basement with a task of cleaning it up. It was mildly put a mess, this photo was taken shortly after refactoring has begun. I was unable to even go inside and therefore forced to put half of the stuff in the hall.
Three months ago I was comparing Lisp, Smalltalk and Ruby. I was struggling to create new language syntax (for example my version of if..else) that is so easy to do in Lisp, and also possible in Smalltalk:
In Smalltalk it would be used like this:
My knowledge of Smalltalk is non-existing, but look at the beautiful  block syntax and notice the keyword arguments being able to accept multiple blocks.
Implementation in Smalltalk is both straightforward and cool:
In Ruby I was missing the ability to have multiple blocks in method call that would enable me to evaluate only one leg. This was best I could come up with and it included (mis)using the lambdas:
Today I’ve read the excellent post about using Ruby Blocks as Dynamic Callbacks. I was mesmerized, but of course I didn’t get it. Than I saw really kick ass explanation here. Holding hands did have effect even on me. Still I am not sure that I would be able to pull it of on my own without watching the original code. At the end I did saw one gist in comments claiming to have similar pattern. I don’t know if it’s better, but it looks less intrusive because there is no patching of Proc class and it clicked more easily.
The decorator pattern is used to extend the functionality of a certain object in a runtime. In a statically typed language you need to define decorator interface, then subclass from it and initialize the component (object) that you are decorating.
In Ruby you can pledge: “Yes I am a grownup” and skip the formal interface definition part.
In either case the payoff comes with the fact that you can combine “extensions” without writing out complex inheritance tree that accounts for all possible combinations. Therefore you end up using extension that you need when you need them.
Let’s check it out on Wikipedia coffee example.
If we want some latte, we can create a new decorator for the coffee: