Language syntax

As noted in my last post, I'm trying to pick a couple of new languages. I'm currently looking at python (not really new to me, but reactivating my knowledge of it), ruby, erlang and C#. Of those four, the one that's strangest to me is ruby - and I've been trying to pinpoint why.

Functions/methods I

Most of the languages I've used have had a pretty similar syntax for naming functions and for calling them. If you leave out assembly, then the typical format (in regex) is:


So, alphanumeric with possibility for underscores. Different languages have different conventions for function naming, though most conventions span languages, and it's just convention anyway. However, in ruby, function names can include some non-alphanumeric characters that you normally wouldn't see in a function name - that is, if you're used to php, javascript, c-like languages, python, etc.

So, sort! is a perfectly legal name in ruby. The exclamation point signifies that the method will change it's object in place. Similarly, ? can be used in function names - this normally signifies that the function will return a boolean value.

Neither symbol can be used other than at the end of function names - but still, it just throws me off. To me, ! and ? are operators, just like *, /, + and % are - they aren't part of names. That means I still see sort! as function + operator at first glance and only after thinking about it do I realize what it is.

Now, the funny thing is, I actually think it's a pretty good naming scheme - or, rather, I think it's a pretty great idea for an operator. For any function, just slap on ! to have it alter the first given parameter in-place instead of return a value. I doubt it would lead to bigger WTF-moments than realizing some schmuck named his function with a ! but it doesn't alter anything in-place (the ! is a convention, nothing more).

Functions/methods II

The other thing that's bugging me about ruby is the syntax for calling a function. Specifically, that function(parameter) and function parameter are both valid. Or, not that they're both valid, but that function parameter is valid and preferred (where not completely illegible). It does make sense that the interpreter will know the difference between a function and other properties/variables and thus will know if it should return the variable or call the function - however, the point is that to me it just looks like you're asking for a property or a variable. Leaving off the parentheses just makes it harder for me to figure out if you're calling a function or not - is [].sort a property (it's Danish for black) or a method?

Granted, sort is not likely to a big problem, but there are still a lot of ruby I don't know - having to guess doesn't make it any easier. And while I could obviously just stick to parentheses myself, I'm highly unlikely to never look at another persons code. If for no other reasons than because I am a fan of DRY.

Incidentally, ruby shares this coffeescript - and it bugs me there too. In fact, coffeescript suffers more from it, in my eyes, because there you can't distinguish between referencing a function that doesn't use parameters and calling the same. Hence, you either need to use parentheses in that case or a different construct to call functions without parameters. Either way, your clean scheme just got compromised.

Variable naming

Continuing from the previous section, ruby has another quirk/feature, that's to do with naming and syntax: to reference a global variable inside a class, you prefix it with a dollar-sign. This actually means the dollar-sign is syntax: without the dollar-sign, the variable won't be global. However, it is also naming: my_variable (in the global scope) and \$my_variable (in class scope) are not the same - even though my_variable in the global scope is exactly that, global!

In some ways, I expect this makes it easier to deal with: you see a name of a variable and you just take the entire string as the name - you don't have to consider if any of it is an operator or something else. But it does mean that if you're used to syntax as something different from what it's used on, then you'll be awfully confused.


I find it interesting that the biggest differences and stumbling blocks I see are in syntax. I might dislike what I see as anal retentiveness in Java and C++, but I understand it quite well and intuitively get it. With ruby I'm suddenly wondering: what exactly am I looking at? In that sense it makes it harder to pick up - at least for me. Then again, I do have some years experience staring at code, so it might also go the other way for beginners, seeing as ruby does come closer to English than most languages I have seen.

However, I also find that the things I stumble on are interesting and could make some things easier. I think what I'm mainly stumbling on is new syntax - a new way of doing things, which I'm not used to. And that's actually cool: having your eyes opened to different ways of doing things.