Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override `File::SEPARATOR`

Documents for File.join states that:

join(string, ...)string

Returns a new string formed by joining the strings using File::SEPARATOR.

File.join("usr", "mail", "gumby") #=> "usr/mail/gumby"

However, the result below shows a different behavior.

File::SEPARATOR #=> "/"
File::SEPARATOR = "doge"
File::SEPARATOR #=> "doge"
File.join("so", "wow") #=> "so/wow"

Could anybody explain what is happening? Is there a way to override this behavior by setting File::SEPARATOR to another value?

I don't have a specific use case for this, nor am I looking for workarounds.. just curious. Thank you in advance.

like image 969
cozyconemotel Avatar asked Jan 29 '16 06:01

cozyconemotel


People also ask

How do I use a file separator?

A file separator is a character that is used to separate directory names that make up a path to a particular location. This character is operating system specific. On Microsoft Windows, it is a back-slash character (), e.g. C:myprojectmyfilesome. properties.

What is the alternative directory separator for Linux?

If you prefer to hard-code the directory separator character, you should use the forward slash ( / ) character. It is the only recognized directory separator character on Unix systems, as the output from the example shows, and is the AltDirectorySeparatorChar on Windows.

What is Pathseparator?

The path separator is a character commonly used by the operating system to separate individual paths in a list of paths.

What is the value of file separator in Java?

File. separator: Platform dependent default name-separator character as String. For windows, it's '\' and for unix it's '/'.


1 Answers

When you define redefine the constant, all future Ruby code will see this new value.

However the implementation of File.join is in C which references the C constant of the separator which you have not redefined.

Any C code will reference the original value (that was set when the Ruby interpreter was initialized) whereas any Ruby code will reference the overridden/redefined value.

like image 53
14 revs, 12 users 16% Avatar answered Oct 26 '22 23:10

14 revs, 12 users 16%