A question of style...

Discussion in 'Ruby' started by Kyle Schmitt, Jul 25, 2007.

  1. Kyle Schmitt

    Kyle Schmitt Guest

    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)

    Kyle Schmitt, Jul 25, 2007
    1. Advertisements

  2. Kyle Schmitt

    cardboard42 Guest

    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?

    cardboard42, Jul 25, 2007
    1. Advertisements

  3. Kyle Schmitt

    Kyle Schmitt Guest

    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)

    def initialize(url)

    def cleanURL()
    Kyle Schmitt, Jul 25, 2007
  4. Kyle Schmitt

    cardboard42 Guest

    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.

    cardboard42, Jul 25, 2007
  5. Kyle Schmitt

    Kyle Schmitt Guest

    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

    I'm just trying to figure out which way will really be best.
    Kyle Schmitt, Jul 25, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.