Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
class method
File.symlink
(::path old_name, ::path new_name) -> 0
File.symlink(target_path, link_path) → 0
file.c
View on GitHub ↗
static VALUE
rb_file_s_symlink(VALUE klass, VALUE from, VALUE to)
{
FilePathValue(from);
FilePathValue(to);
from = rb_str_encode_ospath(from);
to = rb_str_encode_ospath(to);
if (symlink(StringValueCStr(from), StringValueCStr(to)) < 0) {
sys_fail2(from, to);
}
return INT2FIX(0);
}
Not supported on some platforms.
Creates a symbolic link at link_path to the entry at target_path:
Example
filepath = '/etc/passwd' # Regular file. linkpath = '/tmp/foo' File.symlink(filepath, linkpath) File.readlink(linkpath) # => "/etc/passwd" File.read(filepath) == File.read(linkpath) # => true
If the entry at target_path is itself a symbolic link, that link is not followed:
Example
link2path = '/tmp/bar' File.symlink(linkpath, link2path) File.readlink(link2path) # => "/tmp/foo" File.read(filepath) == File.read(link2path) # => true File.delete(linkpath, link2path) # Clean up.