DHH · 2025-07-12 · source ↗ · whole document (17)
Beautiful code
`0:49:14`
because Ruby is written for humans and humans are messy creatures. They like things in just the right way. I can't fully explain why the __init___ make me repulse, but it does. And when I look at the Ruby alternative, it's really instructive. So it's def, same part, D-E-F, space, initialize, parentheses. Not even parentheses. If you don't need to call it within the arguments, there's not even a parentheses. That in itself is actually also a major part. If the human doesn't need the additional characters, we're not just gonna put them in because it'd be nicer to parse for the computer. We're gonna get rid of the semicolons. We're gonna get rid of the parentheses. We're gonna get rid of the underscores. We're gonna get rid of all that ugliness, all the line noise and boil it down to its pure essentials. And at the same time, we're not gonna abbreviate. This is a key difference in the aesthetics between Ruby and Python as well.
`0:50:14`
init, short of type, it's only five characters. Initialize is a lot longer, but it looks a lot better and you don't type it very often. So you should look at something pretty. If you don't have to do it all the time, it's okay that it's long. Those kinds of aesthetic evaluations are rife all over the Ruby language. But let me give you an even better example. The if conditional. That's the bedrock of all programming languages. They have the if conditional. If you take most programming languages, they all have if. That's basically the same in almost every language. Space, start parentheses, we all do that. And then you have perhaps, let's say you're calling a object called user.isadmin, close parentheses, close parentheses, start brackets, and here's what we're gonna do if the user is an admin, right? That would be a normal programming language. Ruby doesn't do it like that.
`0:51:15`
Ruby boils almost all of it away. We start with the if. Okay, that's the same. No parenthesis necessary because there's no ambiguity for the human to distinguish that the next part is just a single statement. So you do if, space, user, dot, admin, question mark. No open brackets, no parentheses, no nothing. Next, open line. Here's your conditional. That question mark means nothing to the computer, but it meets something to the human. Ruby put in the predicate method style purely as a communication tool between humans. It's actually more work for the interpreter to be able to see that this question mark is there. Why is this question mark in here? Because it just reads so nicely. If user.admin?
`0:52:15`
That's a very human phrase, but it gets better. You can turn this around. You can have your statement you want to execute before the conditional. You can do user.upgrade. Say you're calling an upgrade method on a user. Space, if, space, user.admin?. We do the thing if the thing is true. Instead of saying, if the thing is true, do the thing. But it gets even better. This is why I love this example with the conditional, because you can keep diving into it. So let's flip it around. user.downgrade if!not user.admin, right? That'd be a typical way of writing it. Ruby goes, that exclamation point is light noise. Why do we have if and then an exclamation point, that's ugly. We could do user.downgrade unless user.admin?
`0:53:17`
That to me is an encapsulation of the incredible beauty that Ruby affords the programmer through ambiguity that is only to serve the human reader and writer. All of these statements we've just discussed, they're the same for the computer. It'll combine down to the same C code. That'll compile down to the same assembly code. It makes no difference whatsoever. In fact, it just makes it harder to write an interpreter. But for the human who gets to choose whether the statement comes before the conditional or the predicate method has, it's just incredible. It reads like poetry at some point. — It's also incredible that, you know, one language designer's creating that, you know, Guido van Rossum also, it's like one person gets to make these extremely difficult decision because you have to think about how does that all get parsed? And you have to think about the thousands, if it's a popular language that millions of people that end up using this.