# Prefixes \_ and \_\_ for object and class constants proposed

**URL:** <https://www.ruby-forum.com/t/prefixes-and-for-object-and-class-constants-proposed/127177>\
**Category:** Ruby\
**Created:** [January 25, 2008, 8:25pm UTC](https://www.ruby-forum.com/t/prefixes-and-for-object-and-class-constants-proposed/127177 "2008-01-25T20:25:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [January 25, 2008, 8:25pm UTC](https://www.ruby-forum.com/t/prefixes-and-for-object-and-class-constants-proposed/127177/1 "2008-01-25T20:25:31Z")

</div>

I may have brought this up before, a long time ago, but something I want  
is a prefix, like @ and @@, for constants that designates them as object  
or class constants respectively. That way I could make a lot of things  
constants that I am now just making variables and it would be obvious  
that they are constants only available for that context. I really don’t  
like the underscore perfectly, but I’ve thought about it and it seems to  
be the best choice of available characters. Has anybody every thought  
about this (barring my possibly bringing it up two years ago), and does  
anyone else think this is a reasonable suggestion? If so, I would think  
it would be a small change that could be implemented in parallel with  
existing prefix syntax handling.

Sincerely, Xeno C., Tacoma/Seattle programmer.

---

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [January 25, 2008, 8:34pm UTC](https://www.ruby-forum.com/t/prefixes-and-for-object-and-class-constants-proposed/127177/2 "2008-01-25T20:34:26Z")

</div>

On Jan 25, 2008, at 1:24 PM, Xeno C. wrote:

> seems to  
> be the best choice of available characters. Has anybody every thought  
> about this (barring my possibly bringing it up two years ago), and  
> does  
> anyone else think this is a reasonable suggestion? If so, I would  
> think  
> it would be a small change that could be implemented in parallel with  
> existing prefix syntax handling.

I’m not sure I’ve ever bumped into the need for an object constant,  
but if I had, I’d probably have simply implemented an instance method

class Dave  
def initialize(new\_pi)  
@pi = new\_pi  
end  
def PI  
@pi  
end  
end

d1 = Dave.new(3)  
d2 = Dave.new(3.14159)  
d3 = Dave.new(22.0/7.0)

puts d1.PI, d2.PI, d3.PI

Dave

---

<div class="post-metadata">

**Author:** ![Xeno\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/958977/32.png) [@Xeno\_C](https://www.ruby-forum.com/u/Xeno_C)\
**Post date:** [January 25, 2008, 8:53pm UTC](https://www.ruby-forum.com/t/prefixes-and-for-object-and-class-constants-proposed/127177/3 "2008-01-25T20:53:50Z")

</div>

On Jan 25, 2008, at 11:24 AM, Xeno C. wrote:

> seems to  
> be the best choice of available characters. Has anybody every thought  
> about this (barring my possibly bringing it up two years ago), and  
> does  
> anyone else think this is a reasonable suggestion? If so, I would  
> think  
> it would be a small change that could be implemented in parallel with  
> existing prefix syntax handling.

In ruby, constants are more like variables that yell at you.

$ ruby  
X = 1  
X = 2  
p X  
^D  
-:2: warning: already initialized constant X  
2
