Project

General

Profile

Bug #1297

Updated by jeremyevans0 (Jeremy Evans) over 4 years ago

=begin 
  
  かくたにです。 
 
  Pathname#subを使って、ファイルパスからファイル名っぽいものを 
  抜きだそうとしたらうまくいきませんでした。 
 
  http://gist.github.com/80263 
  $ ruby191 -rpathname -ve ' 
  path = "/Users/home/moro/work/xxx/spec/fixtures/me.png" 
  re     = /^.*#{File::SEPARATOR}([^#{File::SEPARATOR}]+)$/ 
  p Pathname.new(path).sub(re){ $1 } # => #<Pathname:> 
  p Pathname.new( Pathname.new(path).instance_variable_get("@path").dup.sub(re){ $1 } ) # => #<Pathname:me.png>' 
  ruby 1.9.1p0 (2009-01-30) [i386-darwin9.6.0] 
  #<Pathname:> 
  #<Pathname:me.png> 
 
  1.8.7でも同様です(というか最初は1.8.7で遭遇)。 
 
  $ ruby187 -rpathname -ve ' 
  path = "/Users/home/moro/work/xxx/spec/fixtures/me.png" 
  re     = /^.*#{File::SEPARATOR}([^#{File::SEPARATOR}]+)$/ 
  p Pathname.new(path).sub(re){ $1 } # => #<Pathname:> 
  p Pathname.new( Pathname.new(path).instance_variable_get("@path").dup.sub(re){ $1 } ) # => #<Pathname:me.png>' 
  ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-darwin9] 
  #<Pathname:> 
  #<Pathname:me.png> 
 
  これは、Rails2.3を使ったアプリケーションでファイルアップロードのテストを書いていて、 
  http://github.com/rails/rails/blob/18eb80ccc7e932f9a6c00462ceaeea648631b120/actionpack/lib/action_controller/test_process.rb 
  の、368行目で遭遇しました。 
 
  @original_filename = path.sub(/^.*#{File::SEPARATOR}([^#{File::SEPARATOR}]+)$/) { $1 } 
 
  ここのpathがPathnameのインスタンスだとマッチしません(Stringだとマッチする)。 
 
  使う側の気持ちとしては、Pathname#subではファイルパスの文字列へのsubと同じ挙動であって欲しいと思いました。 
 
  余談: 
  Rails.rootがRails 2.3からPathnameを返すようになっていて(以前はStringだった)、 
  うっかりRails.root.join("spec", "fixtures", "vacation.jpg") と書いて失敗しました。 
  そんなの聞いてなかったよ……。 
 
 =end 
 

Back