# Decorating "types"

**URL:** <https://www.ruby-forum.com/t/decorating-types/141183>\
**Category:** Ruby\
**Created:** [June 23, 2008, 11:23am UTC](https://www.ruby-forum.com/t/decorating-types/141183 "2008-06-23T11:23:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [June 23, 2008, 11:23am UTC](https://www.ruby-forum.com/t/decorating-types/141183/1 "2008-06-23T11:23:58Z")

</div>

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 = []

again.

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

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  
nice.

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”.)

and

- 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 😉 - but I  
wanted to toss this small idea onto the mailing list anyway.

Regards.
