# Modifying a class method's state

**URL:** <https://www.ruby-forum.com/t/modifying-a-class-methods-state/151644>\
**Category:** Rails\
**Created:** [October 23, 2008, 10:04pm UTC](https://www.ruby-forum.com/t/modifying-a-class-methods-state/151644 "2008-10-23T22:04:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://www.ruby-forum.com/u/nick)\
**Post date:** [October 23, 2008, 10:04pm UTC](https://www.ruby-forum.com/t/modifying-a-class-methods-state/151644/1 "2008-10-23T22:04:19Z")

</div>

Hi guys. I’m experiencing some strange behaviour in my Rails app. I  
have a constant, and two class methods:

#----------#  
COORDINATES\_NOT\_NULL\_SQL =  
‘latitude IS NOT NULL and longitude IS NOT NULL’

def self.sql\_for\_non\_null\_coordinates  
COORDINATES\_NOT\_NULL\_SQL  
end

def self.filtered\_properties(params)  
conditions\_string = self.sql\_for\_non\_null\_coordinates  
…8\<…  
conditions\_string \<\< ’ AND ’ \<\< filtered[:sql]  
…8\<…  
end  
#----------#

If I call #filtered\_properties multiple times in a row, the value that  
#sql\_for\_non\_null\_coordinates returns changes:  
[http://pastie.org/299037](http://pastie.org/299037)

This line is causing the change:  
conditions\_string = self.sql\_for\_non\_null\_coordinates  
But why? And is it possible to prevent this behaviour?

Thanks,  
Nick

---

<div class="post-metadata">

**Author:** ![nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://www.ruby-forum.com/u/nick)\
**Post date:** [October 23, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/modifying-a-class-methods-state/151644/2 "2008-10-23T23:01:59Z")

</div>

On Thu, Oct 23, 2008 at 10:35 PM, Nick [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> #sql\_for\_non\_null\_coordinates returns changes:  
> [http://pastie.org/299037](http://pastie.org/299037)
> 
> This line is causing the change:  
> conditions\_string = self.sql\_for\_non\_null\_coordinates  
> But why? And is it possible to prevent this behaviour?

Hi Nick,  
This is because Ruby constants are not constants. They can change.  
Ruby warns you when you reinitialize a constant, but not otherwise.  
That is,  
Constant \<\< “anything”  
won’t produce a warning.

You should use something like,  
conditions\_string = self.sql\_for\_non\_null\_coordinates.dup  
…

–

- Kazim Z.  
Blog: [http://tuxplayground.blogspot.com](http://tuxplayground.blogspot.com)

---

<div class="post-metadata">

**Author:** ![nick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f9ae1b/32.png) [@nick](https://www.ruby-forum.com/u/nick)\
**Post date:** [October 24, 2008, 5:02pm UTC](https://www.ruby-forum.com/t/modifying-a-class-methods-state/151644/3 "2008-10-24T17:02:06Z")

</div>

On Oct 23, 1:30 pm, “Kazim Z.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Nick,  
> This is because Ruby constants are not constants. They can change.  
> Ruby warns you when you reinitialize a constant, but not otherwise.  
> That is,  
> Constant \<\< “anything”  
> won’t produce a warning.
> 
> You should use something like,  
> conditions\_string = self.sql\_for\_non\_null\_coordinates.dup  
> …

Thanks for the tip, Kazim! That answers my question perfectly.

Cheers,  
Nick
