# A question of style

**URL:** <https://www.ruby-forum.com/t/a-question-of-style/110349>\
**Category:** Ruby\
**Created:** [July 25, 2007, 5:08pm UTC](https://www.ruby-forum.com/t/a-question-of-style/110349 "2007-07-25T17:08:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [July 25, 2007, 5:08pm UTC](https://www.ruby-forum.com/t/a-question-of-style/110349/1 "2007-07-25T17:08:08Z")

</div>

I just had a funny thing happen.  
I’ve got a bunch of classes that take a url for their initialize  
methods, and do a little cleaning on the url, just in case.

I was setting them all up like this  
@url = “[http://localhost:1948/](http://localhost:1948/)”  
@one = pageOne(@url)  
@two = pageTwo(@url)  
@three = pageThree(@url)

Because of the housekeeping & pass-by-reference being standard, @url  
ended up getting longer and longer, and strange and stranger.

I know a dozen fairly easy ways of solving this problem, but my  
question is, what is the _right_ way to solve it in ruby?

I could easily do this in the SuperPage class that all the pages  
inherit from, but it seems a tad awkward. Then again so do some of  
the other ideas I have.

def initailize(url)  
@url=cleanURL(url.clone)  
end

Thanks,  
Kyle

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [July 25, 2007, 5:25pm UTC](https://www.ruby-forum.com/t/a-question-of-style/110349/2 "2007-07-25T17:25:57Z")

</div>

On 7/25/07, Kyle S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def initailize(url)  
> @url=cleanURL(url.clone)  
> end

It looks like your page\* and cleanURL methods are in the same class as  
the  
one you’re initializing? If this is the case why not just access @url  
directly in the methods, and avoid passing it to them entirely?

Thanks,

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [July 25, 2007, 5:40pm UTC](https://www.ruby-forum.com/t/a-question-of-style/110349/3 "2007-07-25T17:40:32Z")

</div>

Tanner,  
It’s not quite the point. At the moment they are being  
accessed directly, bf you’re going to use a clone, to ensure you don’t  
manipulate the var being passed in, you’d either have to do one of the  
following, and that’s a matter of taste.

def initialize(url)  
@url=cleanURL(url.cone)  
…  
end  
or

def initialize(url)  
@url=url.clone  
cleanURL  
…  
end

def cleanURL()  
@url.gsub!(whatever)

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [July 25, 2007, 5:41pm UTC](https://www.ruby-forum.com/t/a-question-of-style/110349/4 "2007-07-25T17:41:41Z")

</div>

On Jul 25, 11:06 am, “Kyle S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Because of the housekeeping & pass-by-reference being standard, @url  
> @url=cleanURL(url.clone)  
> end
> 
> Thanks,  
> Kyle

I think it’s more standard to use String#dup rather than clone. If all  
these pages have access to the cleanURL method why not just do the dup  
in there rather than passing in the copy?

Ken

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [July 25, 2007, 5:46pm UTC](https://www.ruby-forum.com/t/a-question-of-style/110349/5 "2007-07-25T17:46:10Z")

</div>

On Jul 25, 11:38 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > @one = pageOne(@url)  
> > inherit from, but it seems a tad awkward. Then again so do some of  
> > these pages have access to the cleanURL method why not just do the dup  
> > in there rather than passing in the copy?
> 
> Ken

Oh yeah and the rubyish way to do the dup in cleanURL would be like so

def cleanURL(url)  
url = url.dup  
…

And then you can continue on your merry way like nothing changed.

Ken

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [July 25, 2007, 5:49pm UTC](https://www.ruby-forum.com/t/a-question-of-style/110349/6 "2007-07-25T17:49:28Z")

</div>

On 7/25/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I think it’s more standard to use String#dup rather than clone. If all  
> these pages have access to the cleanURL method why not just do the dup  
> in there rather than passing in the copy?
> 
> Ken

That’s a good option. Would it make sense though? Having it in the  
initialize method makes it clear when you start reading the class, but  
having it in the cleanURL method shows it to the user where it is  
used…

I’m just trying to figure out which way will really be best.
