# Namespace pollution in ruby

**URL:** <https://www.ruby-forum.com/t/namespace-pollution-in-ruby/67155>\
**Category:** Ruby\
**Created:** [September 1, 2006, 3:05am UTC](https://www.ruby-forum.com/t/namespace-pollution-in-ruby/67155 "2006-09-01T03:05:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hemant\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@Hemant\_S](https://www.ruby-forum.com/u/Hemant_S)\
**Post date:** [September 1, 2006, 3:05am UTC](https://www.ruby-forum.com/t/namespace-pollution-in-ruby/67155/1 "2006-09-01T03:05:34Z")

</div>

Recently while coding in rails, i had a column called in one of the  
tables  
and hence was wrapped up by ActiveRecord.  
Guess, what it broke entire of the code, because quote is a already  
defined  
method somewhere in rails. I had trouble in finding out the root of the  
problem.

Another example, as a newbie to ruby and rails I was using login\_engine  
in  
my rails code. I have a table called messages in my database. Now,  
login\_engine controller code is like this:

```
  User.transaction(@user) do
    @user.new_password = true
    unless LoginEngine.config(:use_email_notification) and

```

LoginEngine.config(:confirm\_account)  
@user.verified = 1  
end  
if @user.save  
key = @user.generate\_security\_token  
url = url\_for(:action =\> ‘home’, :user\_id =\> @user.id, :key =\>  
key)  
flash[:notice] = ‘Signup successful!’  
if LoginEngine.config(:use\_email\_notification) and  
LoginEngine.config(:confirm\_account)  
UserNotify.deliver\_signup(@user, params[:user][:password],  
url)  
flash[:notice] \<\< ’ Please check your registered email  
account  
to verify your account registration and continue with the login.’  
else  
flash[:notice] \<\< ’ Please log in.’  
end  
redirect\_to :action =\> ‘login’  
end  
end

Guess, what User.transaction was behaving oddly because it was throwing  
warning “Messages already defined constant”. The error was shown in web  
server logs and hence again was a little difficult to track.

Now, how as a new guy to ruby and rails, how do i know , i shouldn’t  
have a  
column called quote and i shouldn’t have a table called messages.

I am a Qt/C++ programmer also, and the namespace issue is mostly  
non-existent there.First of all, its because all the Qt classes begin  
with Q  
and hence you know.But Ruby library authors seems a little lax on this,  
and  
open classes make the entire thing a little nifty and difficult to debug  
if  
you are caught on the wrong foot.Either library authors should avoid  
writting classes like Messages or ruby should give option of importing  
only  
certain methods from a module.  
Something like:

using std::cout;

But still i am new to ruby(3 months precisely), so if you wish you can  
take  
my assertions with a grain of salt.

---

<div class="post-metadata">

**Author:** ![Hemant\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@Hemant\_S](https://www.ruby-forum.com/u/Hemant_S)\
**Post date:** [September 1, 2006, 3:31am UTC](https://www.ruby-forum.com/t/namespace-pollution-in-ruby/67155/2 "2006-09-01T03:31:34Z")

</div>

On Aug 31, 2006, at 9:03 PM, Hemant . wrote:

> login\_engine in  
> my rails code. I have a table called messages in my database. Now,  
> login\_engine controller code is like this:

[snip code]

> I am a Qt/C++ programmer also, and the namespace issue is mostly  
> Something like:
> 
> using std::cout;
> 
> But still i am new to ruby(3 months precisely), so if you wish you  
> can take  
> my assertions with a grain of salt.

This is in some ways more of a Rails problem than ruby problem and  
involves the way rails works, with you inheriting from Rails’  
classes. That particular problem would actually not be solved by C++  
style namespaces or even Q-prefixes. A potential solution to the  
problems you’ve experienced are the idea of selector namespaces which  
have been tossed around on the ML before.

---

<div class="post-metadata">

**Author:** ![Hemant\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@Hemant\_S](https://www.ruby-forum.com/u/Hemant_S)\
**Post date:** [September 1, 2006, 3:43am UTC](https://www.ruby-forum.com/t/namespace-pollution-in-ruby/67155/3 "2006-09-01T03:43:35Z")

</div>

Why not, if rails would have been coded to have proper prefixes in the  
classes/modules, I would have avoided that trouble?

---

<div class="post-metadata">

**Author:** ![Hemant\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@Hemant\_S](https://www.ruby-forum.com/u/Hemant_S)\
**Post date:** [September 1, 2006, 3:55am UTC](https://www.ruby-forum.com/t/namespace-pollution-in-ruby/67155/4 "2006-09-01T03:55:35Z")

</div>

On Aug 31, 2006, at 9:41 PM, Hemant . wrote:

> Why not, if rails would have been coded to have proper prefixes in the  
> classes/modules, I would have avoided that trouble?

class RailsPrefixActiveRecord  
def hello  
… does something useful …  
end  
end

class YourClass \< RailsPrefixActiveRecord  
belongs\_to :hello  
end

Do you see the issue now? Prefixes still wouldn’t have helped your  
problem, unless you wanted to prefix every method call, which is I  
think we can agree ridiculous.

(P.S.: Please don’t top-post)

---

<div class="post-metadata">

**Author:** ![Hemant\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@Hemant\_S](https://www.ruby-forum.com/u/Hemant_S)\
**Post date:** [September 6, 2006, 1:32am UTC](https://www.ruby-forum.com/t/namespace-pollution-in-ruby/67155/5 "2006-09-06T01:32:50Z")

</div>

On Aug 31, 2006, at 18:54, Logan C. wrote:

> end  
> end
> 
> class YourClass \< RailsPrefixActiveRecord  
> belongs\_to :hello  
> end
> 
> Do you see the issue now?

Um, I don’t. Since he said “prefixes IN the  
classes/modules” and not “prefixes OF the classes/modules”, I would  
expect something like

> class RRActiveRecord  
> def rrhello  
> … does something useful …  
> end  
> end

Yes? No? On the other hand, I have no idea what “belongs\_to” does, so  
maybe that’s why this doesn’t make any sense to me…

> Prefixes still wouldn’t have helped your problem, unless you wanted to  
> prefix every method call, which is I think we can agree ridiculous.

Hmm. I’ve somehow not found that ridiculous in the case of Macintosh  
OSX/Cocoa et al., where just about every imaginable object begins with  
NS (for NextStep, as it happens). I collect NSEvents from my NSWindow  
with its NSView with an NSTextField that gives me an NSString. That’s  
really worked pretty well for me so far.

---

<div class="post-metadata">

**Author:** ![Hemant\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@Hemant\_S](https://www.ruby-forum.com/u/Hemant_S)\
**Post date:** [September 6, 2006, 1:56am UTC](https://www.ruby-forum.com/t/namespace-pollution-in-ruby/67155/6 "2006-09-06T01:56:51Z")

</div>

On Sep 5, 2006, at 7:32 PM, Dave H. wrote:

> 

I said prefix every method. Every Cocoa/NextSTEP method is not  
prefixed with NS. You don’t say [@“one.two.three”  
NScomponentsSeparatedByString:@"."] you say [@“one.two.three”  
componentsSeparatedByString: @"."]. Maybe I over-generalized when I  
said we could agree that it was ridicolous, but NextSTEP / Cocoa is  
not an example of prefixing every method call.

---

<div class="post-metadata">

**Author:** ![Hemant\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8797f3/32.png) [@Hemant\_S](https://www.ruby-forum.com/u/Hemant_S)\
**Post date:** [September 1, 2006, 4:09am UTC](https://www.ruby-forum.com/t/namespace-pollution-in-ruby/67155/7 "2006-09-01T04:09:36Z")

</div>

On 9/1/06, Logan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> ... does something useful ...
> 
> ```
> 
> (P.S.: Please don’t top-post)

Yes…agreed.

P.S: sorry about top posting.
