# Hook, or override const\_set, and const\_get

**URL:** <https://www.ruby-forum.com/t/hook-or-override-const-set-and-const-get/116153>\
**Category:** Ruby\
**Created:** [October 2, 2007, 11:43pm UTC](https://www.ruby-forum.com/t/hook-or-override-const-set-and-const-get/116153 "2007-10-02T23:43:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [October 2, 2007, 11:43pm UTC](https://www.ruby-forum.com/t/hook-or-override-const-set-and-const-get/116153/1 "2007-10-02T23:43:51Z")

</div>

I have seen some examples of hooking into const\_missing; Howerver, I  
can’t seem to find anything on const\_set, and const\_get, any ideas?

I tried opening class Module, ex.

class Module  
def const\_set(cname, cval)  
…  
end  
end

Does not work. I realize this is very dangerous. Is there any other  
way to intercept when, ex

FOO=“bar”

happens?

Thanks

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [October 3, 2007, 12:40am UTC](https://www.ruby-forum.com/t/hook-or-override-const-set-and-const-get/116153/2 "2007-10-03T00:40:36Z")

</div>

On Oct 2, 2007, at 14:43 , John P. wrote:

> Does not work. I realize this is very dangerous. Is there any  
> other way to intercept when, ex
> 
> FOO=“bar”
> 
> happens?

a) Edit variable.c

b) Run your code through a ruby2ruby subclass that wraps up your  
const assignments with extra code.

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [October 3, 2007, 3:06am UTC](https://www.ruby-forum.com/t/hook-or-override-const-set-and-const-get/116153/3 "2007-10-03T03:06:50Z")

</div>

Eric H. wrote:

> > end  
> > b) Run your code through a ruby2ruby subclass that wraps up your const  
> > assignments with extra code.

Thanks Eric. Option a makes me think I am taking the wrong approach.  
Option b looks more attractive. Well at least I know I was not missing  
something easier.

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [October 3, 2007, 9:47am UTC](https://www.ruby-forum.com/t/hook-or-override-const-set-and-const-get/116153/4 "2007-10-03T09:47:19Z")

</div>

On Oct 2, 2007, at 18:05 , John P. wrote:

> > > ```
> > > ....
> > > 
> > > ```
> > 
> > b) Run your code through a ruby2ruby subclass that wraps up your  
> > const assignments with extra code.
> 
> Thanks Eric. Option a makes me think I am taking the wrong  
> approach. Option b looks more attractive. Well at least I know I  
> was not missing something easier.

Both options are pure evil and guaranteed to cause you pain.

What is your ultimate goal?
