Ampelofilosofies

homeaboutrss
The mind is like a parachute. If it doesn't open, you're meat.

ForgetMeNot: File.join weirdness

04 Dec 2013

I’m busy with a paid-for-by-an-actual-client system based on the Renesas RX630 so the RX63N adventure has been tabled for a while.

Still I stumbled on a little Ruby weirdness today that I have to take down just in case.

So when handling paths, doing a File.join(“/foo/foo”,”../../bar”) should give /bar back.

If by some twist of luck there’s a space before those first two dots though, the results are quite unexpected:

2.0.0p195 :008 > File.expand_path(File.join('/foo/foo'," ../../bar"))
 => "/foo/foo/bar"
 

The dots are somehow stripped away. Btw. when doing the same with only one directory level:

2.0.0p195 :010 > File.expand_path(File.join('/foo',"../../bar"))
 => "/bar"
2.0.0p195 :011 > File.expand_path(File.join('/foo'," ../../bar"))
 => "/foo/bar"

Also, this only happens when there are two sets of dots (“../../”). When there is only one then the sneaky whitespace is easier to detect:

2.0.0p195 :012 > File.expand_path(File.join('/foo'," ../bar"))
 => "/foo/ ../bar"