From: RichardOnRails Date: 2009-05-13T15:35:02+09:00 Subject: Re: Question on redefining the File.join mechanism 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