From: "Brian Schröder" Date: 2004-10-06T04:48:00+09:00 Subject: Re: A ruby course Leo wrote: > Brian Schr�der wrote: > > >>Hello Group, >> >>I need more help. >> >>Anybody has an idea for an exercise on extending existing classes. > > > The code below introduce a new string concatenation operator '/' which > concatenates two strings placing '/' beween them. This is useful > when forming path from directory and filename components. > > class String > def /(rhs) > self.sub(%r{/+$}, '') + '/' + rhs.sub(%r{^/+}, '') > end > end if > > Now you can write the following code: > > $dir='/usr/local/bin' > $name='ruby' > $full_path= $dir/$name > > Also the '/' operator will make sure that the strings are joined by one and > only one slash. Example: > $dir='/badby/formed/dir///' > > $name='///foo' > > $dir/$name => '/badby/formed/dir/foo' > > Note, that poatterns like '///' were removed. > > Hope it helps, > Leo > That is short and cool. I think this could be an example for me! Thanks, Brian -- Brian Schr�der http://ruby.brian-schroeder.de/