Ruby Quiz (Advanced) #2

question mark

Question

What would be the output of this code snippet?

Solution

The output is:

derived
base
derived
base

That might appear as a surprising behavior for a class variable, which is in general shared between all derived classes. But the trick here is that the class variable var has been set in the child first (and not in the parent first). The implication is that the parent and child classes end up with their own independent class variable (with identical names).

Credit: this is trick #19 from a nice list from Auckland University, New Zealand.

Advertisements

Got a comment?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: