views:

373

answers:

2

My development environment is in Windows, and my production of rails is in Linux.

There are possibility that VirtualHost will be use. Assume that one finename need to call in /public folder with File.open('/tmp/abc.txt', 'r').

but in my Windows it should be "C:\tmp\abc.txt". How can i do a right join path with two difference environment?

prefix_tmp_path = '/tmp/'
filename = "/#{rand(10)}.txt"

fullname = prefix_tmp_path + filename  # /tmp//1.txt <- which i don't want double //

and if prefix_tmp_path = "C:\tmp\" # C:\tmp\/1.txt

is it possible to do a right joining?

+8  A: 

I recommend using File.join

>> File.join("path", "to", "join")
=> "path/to/join"
csexton
+7  A: 

One thing to note. Ruby uses a "/" for file separator on all platforms, including Windows, so you don't actually need use different code for joining things together on different platforms. "C:/tmp/1.text" should work fine.

File.join() is your friend for joining paths together.

prefix_tmp_path = 'c:/tmp'
filename = "#{rand(10)}.txt"
fullname = File.join(prefix_tmp_path,filename)  #c:/tmp/3.txt
Daniel Von Fange