Architecture

Subclass this

Subclass this

In object-oriented languages there’s a nifty feature called code inheritance. People tend to think that this is a really cool ™ thing, since it allows you to quickly slab some new features onto a class whenever the need arises. In fact, there still seems to be a lot of folks who think that the whole point of object-orientation is to subclass a lot.

I’ve previously worked with Microsoft MFC, which was designed that way (but remember, that was back 1992) – as was the class hierarchy that makes your Unreal Tournament tick. I’ve seen object hierarchies spreading over 15 levels, and more.

The only problem with code written that way is that it sucks. A lot.

Read more…

Strange variable scopes – with metaprogramming (part 2)

Strange variable scopes – with metaprogramming (part 2)

This is the second part about my strange findings on variable scopes and metaprogramming in Ruby. This is the part that contains the scary stuff. (Go to the first part)

Read more…

Strange variable scopes – Class variables (part 1)

Strange variable scopes – Class variables (part 1)

There we go again. Ruby has some, err… “unique” features when to the scope of variables. It really starts out really harmless, though…

This is part one of two – this one is just to give you a bit of background for the next one. There’s not much surprising stuff there, so if you want to go for the stranger bits, just jump to the next installment.

Adding methods at runtime

Adding methods at runtime

One of the things that seems to draw people to Ruby is the ability to create and change all things at metprogrammatically at runtime. This in turn allows users to create things like domain-specific languages and the like.

I’m possibly overusing these features at the moment, because it’s a cool new thing ™.

So, let’s see how to add methods at runtime. Since this is Ruby, there are multiple ways to do this, making things more confusing.

(See also the follow-up article)

Ruby overloading

Ruby overloading

A new thing on my quest to learn the new language is weird inconsistency that bugged me lately: In Ruby you can overload virtually everything. That is, everything except the “equals” operator.

In spite of this, half of the Rubyists seem to believe that they overload the “equals” sign all the time.

Still, if you have this kind of expression:

variable_1 = variable_2

it will assign the value of variable_2 to variable_1, and there’s no way to change this behaviour.

However, if have something that looks like this

object.method = expression

the code will actually be evaluated equivalent to

object.send(“method=”, expression)

Read more…