ActiveSupport: thanks, try, tap
The more time I spend with the Rails source, the more I appreciate it. I’ve always appreciated it for what it could do, but now I’m appreciating it directly, for what it is. I find it to be quite elegant, pragmatic, aesthetically pleasing, and educational. Inside the black box, it’s really quite nice.
So…thank you to all of the people that continue to make it better and to DHH and 37Signals for leading the charge. It’s a really great framework to use and study.
The first source fragment for today is a method responsible for URL generation of query strings from a
Hash…something which I’m confident I’ve rolled my own at least once. It’s important to realize that many of the core objects have methods like
to_query and that they all work in concert to handle the arbitrary structure we might throw at it, but we are just examining this one. Check out to_param.rb and to_query.rb for more type specific implementations.
collect with a block on a
Hash gives easy access to key value pairs. There are many ways to do that, but it’s neat to learn another way. It is also interesting that the block also has a postfix operation tacked onto it. Take a look at
end.sort! * '&'. We can reason pretty easily about what that does (sort in place and join the array with ampersands).
That said, I don’t think I’ve ever used
array * str before and I rarely postfix a multiline block. I think I have been using far more temporary variables than I really need to…
See the Array docs for more details.
I know that I have littered my code with existence checks…things like:
Thankfully, there’s a better way baked into every object…try!
So for my egregious code above, I can use the cleaner:
This next code fragment has a method I have never seen before. How cool is that?
Ok, so the actual presence of
String#indent is rather helpful and it does pretty much exactly what you think it does…but hold up…what is
From the docs:
Yields x to the block, and then returns x. The primary purpose of this method is to “tap into” a method chain, in order to perform operations on intermediate results within the chain.
This clever bit of code seems to come from MenTaLguY where you’ll also find other cool uses for it described.
Like, simple debugging without breaking stuff:
In the case of indent, since it taps into a destructive method (
indent!) it changes the string while remaining chainable.
That’s all for now, have fun reading the source!