Question on redefining the File.join mechanism

Discussion in 'Ruby' started by RichardOnRails, May 12, 2009.

  1. I'm running Ruby over WindowsXP-Pro/SP2. So I wanted File.join to use
    backslash.

    I tried:

    class WFile < File
    def join(arg1, arg2)
    super.join(arg1, arg2).gsub(/\//, '\\')
    end
    end

    but WFile.join(s1,s2) didn't work. My question is why?

    BTW, I don't want to start or enter into a debate about the wisdom
    nor aesthetics of this approach.

    Also, I got the File.wjoin to work (using the code below), so I'm
    happy. I just want to learn why my first idea is flawed.

    class File # Join with a backslash rather than a forward slash
    def File.wjoin(arg1, arg2)
    join(arg1, arg2).gsub(/\//, '\\')
    end
    end

    Thanks in Advance,
    Richard
     
    RichardOnRails, May 12, 2009
    #1
    1. Advertising

  2. RichardOnRails

    Marc Heiler Guest

    > I just want to learn why my first idea is flawed.

    I believe one culprit is probably
    super.join

    In fact, I never saw super.join, only super() on something.

    Where did you see super.something ?
    --
    Posted via http://www.ruby-forum.com/.
     
    Marc Heiler, May 12, 2009
    #2
    1. Advertising

  3. 2009/5/12 RichardOnRails <>:
    > I'm running Ruby over WindowsXP-Pro/SP2. =A0So I wanted File.join to use
    > backslash.
    >
    > I tried:
    >
    > class WFile < File
    > =A0def join(arg1, arg2)
    > =A0 =A0super.join(arg1, arg2).gsub(/\//, '\\')


    That line should look like one of these variants:

    super.gsub(/\//, '\\')
    super(arg1, arg2).gsub(/\//, '\\')

    However, since join is a class method you would want to define it as
    class method in WFile.

    irb(main):001:0> class WFile < File
    irb(main):002:1> def self.join(*a) super.gsub(%r{/}, '\\\\') end
    irb(main):003:1> end
    =3D> nil
    irb(main):004:0> WFile.join "foo", "bar", "baz"
    =3D> "foo\\bar\\baz"
    irb(main):005:0>

    > =A0end
    > end
    >
    > but WFile.join(s1,s2) didn't work. =A0My question is why?
    >
    > BTW, =A0I don't want to start or enter into a debate about the wisdom
    > nor aesthetics of this approach.
    >
    > Also, =A0I got the File.wjoin to work (using the code below), so I'm
    > happy. =A0I just want to learn why my first idea is flawed.
    >
    > class File # Join with a backslash rather than a forward slash
    > =A0def File.wjoin(arg1, arg2)
    > =A0 =A0join(arg1, arg2).gsub(/\//, '\\')
    > =A0end
    > end


    Another however: since you are defining a single method only the
    question is whether it warrants a comlete class. Why not just define
    wjoin in class File or simply override File's definition, e.g.

    def File.join(*a)
    a.join '\\'
    end

    Kind regards

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, May 12, 2009
    #3
  4. > In fact, I never saw super.join, only super() on something.
    >
    > Where did you see super.something ?


    Must have been in a dream :) Please see my solution in my reply to
    Robert.

    Thanks for your response,
    Richard
     
    RichardOnRails, May 13, 2009
    #4
  5. On May 12, 10:34 am, Robert Klemme <> wrote:
    > 2009/5/12 RichardOnRails <>:
    >
    > > I'm running Ruby over WindowsXP-Pro/SP2.  So I wanted File.join to use
    > > backslash.

    >
    > > I tried:

    >
    > > class WFile < File
    > >  def join(arg1, arg2)
    > >    super.join(arg1, arg2).gsub(/\//, '\\')

    >
    > That line should look like one of these variants:
    >
    > super.gsub(/\//, '\\')
    > super(arg1, arg2).gsub(/\//, '\\')
    >
    > However, since join is a class method you would want to define it as
    > class method in WFile.
    >
    > irb(main):001:0> class WFile < File
    > irb(main):002:1> def self.join(*a) super.gsub(%r{/}, '\\\\') end
    > irb(main):003:1> end
    > => nil
    > irb(main):004:0> WFile.join "foo", "bar", "baz"
    > => "foo\\bar\\baz"
    > irb(main):005:0>
    >
    >
    >
    > >  end
    > > end

    >
    > > but WFile.join(s1,s2) didn't work.  My question is why?

    >
    > > BTW,  I don't want to start or enter into a debate about the wisdom
    > > nor aesthetics of this approach.

    >
    > > Also,  I got the File.wjoin to work (using the code below), so I'm
    > > happy.  I just want to learn why my first idea is flawed.

    >
    > > class File # Join with a backslash rather than a forward slash
    > >  def File.wjoin(arg1, arg2)
    > >    join(arg1, arg2).gsub(/\//, '\\')
    > >  end
    > > end

    >
    > Another however: since you are defining a single method only the
    > question is whether it warrants a comlete class.  Why not just define
    > wjoin in class File or simply override File's definition, e.g.
    >
    > def File.join(*a)
    >   a.join '\\'
    > end
    >
    > Kind regards
    >
    > robert
    >
    > --
    > remember.guy do |as, often| as.you_can - without endhttp://blog.rubybestpractices.com/


    Hi Robert,

    Thanks to Marc in the previous post and to you, my head cleared and
    following is the solution I sought, which is a style that suits my
    aesthetics :) :

    class WFile
    def WFile.join(arg1, arg2)
    File.join(arg1, arg2).gsub(/\//, '\\')
    end
    end

    puts WFile.join('foo', 'bar') # => foo\bar

    Ya gotta love comp.lang.ruby.

    Best wishes,
    Richard
     
    RichardOnRails, May 13, 2009
    #5
  6. On May 12, 10:34 am, Robert Klemme <> wrote:
    > 2009/5/12 RichardOnRails <>:
    >
    > > I'm running Ruby over WindowsXP-Pro/SP2.  So I wanted File.join to use
    > > backslash.

    >
    > > I tried:

    >
    > > class WFile < File
    > >  def join(arg1, arg2)
    > >    super.join(arg1, arg2).gsub(/\//, '\\')

    >
    > That line should look like one of these variants:
    >
    > super.gsub(/\//, '\\')
    > super(arg1, arg2).gsub(/\//, '\\')
    >
    > However, since join is a class method you would want to define it as
    > class method in WFile.
    >
    > irb(main):001:0> class WFile < File
    > irb(main):002:1> def self.join(*a) super.gsub(%r{/}, '\\\\') end
    > irb(main):003:1> end
    > => nil
    > irb(main):004:0> WFile.join "foo", "bar", "baz"
    > => "foo\\bar\\baz"
    > irb(main):005:0>
    >
    >
    >
    > >  end
    > > end

    >
    > > but WFile.join(s1,s2) didn't work.  My question is why?

    >
    > > BTW,  I don't want to start or enter into a debate about the wisdom
    > > nor aesthetics of this approach.

    >
    > > Also,  I got the File.wjoin to work (using the code below), so I'm
    > > happy.  I just want to learn why my first idea is flawed.

    >
    > > class File # Join with a backslash rather than a forward slash
    > >  def File.wjoin(arg1, arg2)
    > >    join(arg1, arg2).gsub(/\//, '\\')
    > >  end
    > > end

    >
    > Another however: since you are defining a single method only the
    > question is whether it warrants a comlete class.  Why not just define
    > wjoin in class File or simply override File's definition, e.g.
    >
    > def File.join(*a)
    >   a.join '\\'
    > end
    >
    > Kind regards
    >
    > robert
    >
    > --
    > remember.guy do |as, often| as.you_can - without endhttp://blog.rubybestpractices.com/


    Hi, again, Robert,

    Just a little follow-up. When I first read your response, I grabbed
    your "class method" observation and ran with it. I saw your
    additional 3-liner, but didn't understand it at first glance.

    Now I see that you've reduced the matter to Array#join, so I could
    simple write:

    ['foo', 'bar'].join '\\' # => foo\bar,

    but wrapping in a 3-liner like yours probably simplifies invocation.
    I'll think about it some more. Your 3-liner looks like one more "best
    practice", which site I just visited.

    Thanks for education in Ruby elegance.

    Best wishes,
    Richard
     
    RichardOnRails, May 13, 2009
    #6
  7. On 13.05.2009 08:31, RichardOnRails wrote:
    > Now I see that you've reduced the matter to Array#join, so I could
    > simple write:
    >
    > ['foo', 'bar'].join '\\' # => foo\bar,


    Exactly.

    > but wrapping in a 3-liner like yours probably simplifies invocation.
    > I'll think about it some more. Your 3-liner looks like one more "best
    > practice", which site I just visited.
    >
    > Thanks for education in Ruby elegance.


    You're welcome! Just another remark: it's been a while that I used the
    Windows version of Ruby (I work mostly on cygwin and Linux) but as far
    as I remember if you need those file names only internally (i.e. in the
    Ruby program) then the regular File.join will do as you can use forward
    and backward slashes.

    Kind regards

    robert


    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, May 13, 2009
    #7
  8. > ... then the regular File.join will do as you can use
    > forward and backward slashes.


    That has been my experience, but I don't like relying on that and
    finally decided to fix it to my liking.

    Thanks again and best wishes,
    Richard
     
    RichardOnRails, May 14, 2009
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Silver
    Replies:
    0
    Views:
    890
    Alan Silver
    Jun 5, 2006
  2. syd
    Replies:
    2
    Views:
    262
    Greg Ewing
    Apr 8, 2005
  3. googleboy
    Replies:
    1
    Views:
    929
    Benji York
    Oct 1, 2005
  4. Martin Bootsma

    Easy question about redefining a function

    Martin Bootsma, Jul 6, 2006, in forum: C Programming
    Replies:
    6
    Views:
    282
    Rouben Rostamian
    Jul 6, 2006
  5. Xeno Campanoli
    Replies:
    8
    Views:
    174
    Ron Fox
    May 27, 2008
Loading...

Share This Page