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.
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.
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.
The path separator is a character commonly used by the operating system to separate individual paths in a list of paths.
File. separator: Platform dependent default name-separator character as String. For windows, it's '\' and for unix it's '/'.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With