# Railsy Question about redefining ruby methods

**URL:** <https://www.ruby-forum.com/t/railsy-question-about-redefining-ruby-methods/138533>\
**Category:** Ruby\
**Created:** [May 24, 2008, 4:11am UTC](https://www.ruby-forum.com/t/railsy-question-about-redefining-ruby-methods/138533 "2008-05-24T04:11:36Z")\
**Posts on this page:** 9\
**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:** [May 24, 2008, 4:11am UTC](https://www.ruby-forum.com/t/railsy-question-about-redefining-ruby-methods/138533/1 "2008-05-24T04:11:36Z")

</div>

Please this is a Ruby question, not, at least as the main focus, a rails  
one, as you’ll see if you read all the way through.

Say, I’ve got a case in Rails where I want all my links to be prepended  
with something like ~u1/rrsw/thingies so I can see  
[http://host/~u1/rrsw/thingies](http://host/~u1/rrsw/thingies) and [http://host/~u2/rrsw/thingies](http://host/~u2/rrsw/thingies) instead  
of [http://host:9001/thingies](http://host:9001/thingies) and [http://host:9002/thingies](http://host:9002/thingies) for a rails  
tool that is used by some n projects. The rails part is apparently not  
configurable, so I’m looking at redefining rails methods like link\_to to  
stick the path in there. This should be reasonable, but I’m stuck on a  
ruby problem:

I want to redefine the link\_to method so it’s seen in its new guise in  
all the places where it’s presently used. It seems to make most sense  
to put the new redefinition in the module ApplicationHelpers, but I  
believe that may short circuit the access, as I’m not seeing the example  
I put in which always puts out a traceable constant, resulting in the  
expected changes in my test web pages. So, and here is the question:

Can anyone suggest how I should specify an appropriately available  
method for the correct module inside another module so that it overrides  
the original in the fashion I desire? I presume this is a simple  
problem, as it’s what people do all the time in OO right? I’m still a  
little green on these patterns yet I think.

Thanks for any suggestions.

Sincerely, Xeno C.

Droopy hacker on a Friday Evening…

---

<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:** [May 24, 2008, 8:11am UTC](https://www.ruby-forum.com/t/railsy-question-about-redefining-ruby-methods/138533/2 "2008-05-24T08:11:17Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Xeno C. wrote:  
| Please this is a Ruby question, not, at least as the main focus, a rails  
| one, as you’ll see if you read all the way through.  
|  
| Say, I’ve got a case in Rails where I want all my links to be prepended  
| with something like ~u1/rrsw/thingies so I can see  
| [http://host/~u1/rrsw/thingies](http://host/~u1/rrsw/thingies) and [http://host/~u2/rrsw/thingies](http://host/~u2/rrsw/thingies) instead  
| of [http://host:9001/thingies](http://host:9001/thingies) and [http://host:9002/thingies](http://host:9002/thingies) for a rails  
| tool that is used by some n projects. The rails part is apparently not  
| configurable, so I’m looking at redefining rails methods like link\_to to  
| stick the path in there. This should be reasonable, but I’m stuck on a  
| ruby problem:

You are aware that the port of the URL is not under Rails’ nor Ruby’s  
control per se (it is the domain of the webserver)?

URLs are composed in (roughly) six parts: the  
[protocol]://[host]:[port]/[user\_directory]/[directory/that/app/uses/or/rewrites]/[document\_requested.suffix]

Of those, only the directory and explicit document are under control by  
any given web application.

Further, you can only have one server listen on any given port (though,  
one server can listen on an unlimited number of ports).

What you want, can be achieved by using Apache + mod\_rails.

Uou can get the ~u1/~u2 part, by having different users on a, for  
example, UNIX based OS have their own web\_root (or web\_home, as it can  
be called, by convention), which would be reachable as  
host/~username/index.html (for example).

Details of implementing such a setup can be found in various ways on,  
for example, [http://howtoforge.org](http://howtoforge.org).

As far as overriding a Ruby method: You can usually achieve that by  
reopening the class, adding an alias for the old method, and defining  
your own implementation. Be careful, though, when you do.

Where to do that so Rails picks up a change to link\_to, I do not know (I  
suspect it is in ActiveSupport, though).

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)  
Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)

~ - You know you’ve been hacking too long when…  
…your complexion has turned pale from being constantly irradiated by  
the  
monitor.  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkg3sU8ACgkQbtAgaoJTgL/zGgCcD/x24SNTx8OFslWJ5fX5A5gq  
x8UAniPPYbQl1pPj/ytTyFeoxS5Hp7N7  
=Z9nu  
-----END PGP SIGNATURE-----

---

<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:** [May 25, 2008, 1:56am UTC](https://www.ruby-forum.com/t/railsy-question-about-redefining-ruby-methods/138533/3 "2008-05-25T01:56:00Z")

</div>

Phillip G. wrote:

> | of [http://host:9001/thingies](http://host:9001/thingies) and [http://host:9002/thingies](http://host:9002/thingies) for a rails  
> | tool that is used by some n projects. The rails part is apparently not  
> | configurable, so I’m looking at redefining rails methods like link\_to to  
> | stick the path in there. This should be reasonable, but I’m stuck on a  
> | ruby problem:

No no no no no no! I have all the other stuff working. I’ve got  
UserDir and ProxyPass directives that get all the stuff set up initially  
from my [http://host](http://host):n to my [http://host/~u1/rrsw](http://host/~u1/rrsw) like I want. All I  
need now is to get all the URLs that are presently rendered in the  
markup as /something to be ~u1/rrsw/something. That is all I need. I  
have the rest working.

So, thank you for your feedback. It is interesting for other reasons,  
but what I was asking about was the very thing you said you were unable  
to answer; nothing more.

Sincerely, Xeno

xc

---

<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:** [May 25, 2008, 2:28am UTC](https://www.ruby-forum.com/t/railsy-question-about-redefining-ruby-methods/138533/4 "2008-05-25T02:28:09Z")

</div>

Hi –

On Sat, 24 May 2008, Xeno C. wrote:

> Please this is a Ruby question, not, at least as the main focus, a rails one,  
> as you’ll see if you read all the way through.

Well, if my answer is correct, it’s a very Rails-specific question 🙂

> new redefinition in the module ApplicationHelpers, but I believe that may  
> short circuit the access, as I’m not seeing the example I put in which always  
> puts out a traceable constant, resulting in the expected changes in my test  
> web pages. So, and here is the question:
> 
> Can anyone suggest how I should specify an appropriately available method for  
> the correct module inside another module so that it overrides the original in  
> the fashion I desire? I presume this is a simple problem, as it’s what  
> people do all the time in OO right? I’m still a little green on these  
> patterns yet I think.

Have a look at relative\_url\_root, which I believe (though I haven’t  
used it in a while) does what you want.

David

---

<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:** [May 25, 2008, 2:13am UTC](https://www.ruby-forum.com/t/railsy-question-about-redefining-ruby-methods/138533/5 "2008-05-25T02:13:32Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Xeno C. wrote:

| No no no no no no! I have all the other stuff working. I’ve got  
| UserDir and ProxyPass directives that get all the stuff set up initially  
| from my [http://host](http://host):n to my [http://host/~u1/rrsw](http://host/~u1/rrsw) like I want. All I  
| need now is to get all the URLs that are presently rendered in the  
| markup as /something to be ~u1/rrsw/something. That is all I need. I  
| have the rest working.

Why? Is the application not working? The webserver is smart enough to  
construct the link properly, so that links should Just Work™.

And you don’t really want absolute links, either, as this will break  
once you move the application to another server.

Otherwise, look at Rails’ routes.rb.

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)  
Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)

Youth – not a time of life but a state of mind… a predominance of  
courage over timidity, of the appetite for adventure over the love of  
ease.  
~ – Robert Francis Kennedy (1925-1968)  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkg4rxoACgkQbtAgaoJTgL/YBACgpy5rJGR6uwqjmVNp2254HsrH  
2SQAoKccu01YWo+xnag0qY2GTeKqYKCm  
=oIi1  
-----END PGP SIGNATURE-----

---

<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:** [May 25, 2008, 2:37am UTC](https://www.ruby-forum.com/t/railsy-question-about-redefining-ruby-methods/138533/6 "2008-05-25T02:37:31Z")

</div>

Phillip G. wrote:

> | have the rest working.
> 
> Why? Is the application not working? The webserver is smart enough to  
> construct the link properly, so that links should Just Work™.
> 
> And you don’t really want absolute links, either, as this will break  
> once you move the application to another server.

They aren’t absolute at all. What I get rendered by rails link\_to, for  
instance, is things like [Something](https://www.ruby-forum.com/something). What I  
need is still a relative link, which is Something for u1, and Something for u2. I originally expected  
there would be a simple rendering configuration for this in rails, and  
spent much of yesterday and part of Thursday asking on the #rubyonrails  
IRC. Not only did those folks know of no such configuration, but they  
didn’t seem to even understand the OO need for encapsulation, nor the  
logical reason, nor the traditional Unix Webserver usage of ~user,  
nowadays configured with the UserDir Apache directive. Anyway, those  
are mostly Rails oriented issues and problems, which are interesting,  
but not completely appropriate here. What is appropriate to address  
here is the whole idea of redefinition of a Ruby method like link\_to  
which is embedded in some arbitrary community/tree of modules and/or  
classes. I’ve never done much of that, and I know it’s part of the real  
power that experienced OO users acquire in such environments.

Now ideally it would be nice if some Rails supergeek came out of the  
woodwork and said there in fact was a configuration to change the  
rendering, but it looks like there’s not. You wouldn’t want to change  
the URLs that Rails renders with an external rewrite in Apache if you  
can help it because that breaks encapsulation, which is a standard and a  
good ethic in OO programming any reasonable engineer would recognize,  
and given the ability to simply redefine link\_to, url\_for, or whatever  
other methods I use can achieve what I want, and not only that perhaps  
help lead to a future Rails configurability on such things in a  
reasonably elegant matter, presuming that it in fact doesn’t yet exist,  
seems to me to make that the proper direction for me at this point.

Thanks for asking.

---

<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:** [May 25, 2008, 10:49pm UTC](https://www.ruby-forum.com/t/railsy-question-about-redefining-ruby-methods/138533/7 "2008-05-25T22:49:51Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Sat, 24 May 2008, Xeno C. wrote:

I’m cutting this short and just saying that David Black’s suggestion  
works regarding:

> Have a look at relative\_url\_root, which I believe (though I haven’t  
> used it in a while) does what you want.

Since this is a ruby group, and the suggestion is rails, I am going to  
top off my discussion directly to my two correspondencts.

Thank you.

---

<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:** [May 25, 2008, 2:39am UTC](https://www.ruby-forum.com/t/railsy-question-about-redefining-ruby-methods/138533/8 "2008-05-25T02:39:16Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Sat, 24 May 2008, Xeno C. wrote:
> 
> > Please this is a Ruby question, not, at least as the main focus, a  
> > rails one, as you’ll see if you read all the way through.
> 
> Well, if my answer is correct, it’s a very Rails-specific question 🙂

Thank you David. I’ll look at this right now, and try to have an answer  
by tomorrow afternoon, as my evening is about to draw me away.’

Sincerely, Xeno

---

<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:** [May 27, 2008, 1:40pm UTC](https://www.ruby-forum.com/t/railsy-question-about-redefining-ruby-methods/138533/9 "2008-05-27T13:40:34Z")

</div>

Have you looked at the rails routes table?
