Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > A question of style...

Reply
Thread Tools

A question of style...

 
 
Kyle Schmitt
Guest
Posts: n/a
 
      07-25-2007
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/"
@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

 
Reply With Quote
 
 
 
 
cardboard42@gmail.com
Guest
Posts: n/a
 
      07-25-2007
On Jul 25, 11:06 am, "Kyle Schmitt" <(E-Mail Removed)> wrote:
> 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/"
> @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


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

 
Reply With Quote
 
 
 
 
Kyle Schmitt
Guest
Posts: n/a
 
      07-25-2007
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)

 
Reply With Quote
 
cardboard42@gmail.com
Guest
Posts: n/a
 
      07-25-2007
On Jul 25, 11:38 am, "(E-Mail Removed)" <(E-Mail Removed)>
wrote:
> On Jul 25, 11:06 am, "Kyle Schmitt" <(E-Mail Removed)> wrote:
>
>
>
> > 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/"
> > @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

>
> 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


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

 
Reply With Quote
 
Kyle Schmitt
Guest
Posts: n/a
 
      07-25-2007
On 7/25/07, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments