Decorating "types"

One can, in ruby, easily do something like this:

@array_foobar = []
@array_foobar << ‘bla’
@array_foobar << ‘ble’

Most, if not everyone I know, will naturally omit the array_ part, and
I even see some people that like to use @a @b one char style arrays.

Personally though, I like to use this, and if anyone dislikes it, it
is easy to replace this anyway by simple grepping through files, so
that one will have

@foobar = []


Now I was thinking… ruby handles constants somewhat losely, in that
you can reassign a constant. You can even “cheat” and use constants as

BLA = []
BLA << ‘bla’
BLA << ‘ble’
BLA = {}
BLA[‘dog’] = ‘sad’

You see that BLA serves as array and later as hash.
In the second situation ruby will issue a warning, but do as you “told”
it to do so.

I am not sure if anyone uses this, but I found it somewhat

What I was thinking lately now, are these two things basically:

  • A way to make a “constant” really a constant, in that it can never be
    changed again (at least not easily, like ruby currently allows you to
    do. If you change a constant, you get a warning, but that’s it. What I
    mean is that a user should be able to set how “changable” a constant
    should be for a given ruby project, and in certain situations he simply
    never wants the “type” of a variable to change, or what it points to. It
    would be in a way like a frozen “pointer”.)


  • to allow automatic decoration for certain “types” in ruby, which can
    never again change.

Consider the above example in a “pseudo language” construct to
illustrate what i mean:

denote_type ‘array_’, :behaves_as_array
denote_type ‘hash_’, :behaves_as_hash

@array_foobar = []
@array_foobar << ‘bla’

@array_foobar = {}

Some error appears now,

@hash_barfoo = {}

we can fill it up

@hash_barfoo = []

but it can not become anything else than a hash

I guess it would be easy to have a cleanup method too to get rid of
these again if one wants to, but this is not the important thing here.

The key here would be that a user can choose to use this, if he wants to
(since it is purely optional) and use pretty much any decorator aside by
just using the variable name. Additionally he could denote constants as
being really a constant (this is somewhat related to the above code,
although I am using @instance_vars)

I dont really want any extra keyword in ruby (i think it is better if a
language has as few keywords as possible, so I am not proposing at all
to inflate keywords. Noone wants to see Java 2.0 in extra boilerplate
code … )

I am however not sure that:

  • this is even possible
  • this yields ANY advantage whatsoever
  • anyone else WANTS to have it, anyway

So basically I guess the amount of people who would like this is
miniscule - aside from the numerous people who would most likely be
extremely skeptic against anything new, I remember how many disliked the
switch in ruby 1.9 towards “foobar”[0] getting the first char :wink: - but I
wanted to toss this small idea onto the mailing list anyway.