Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
class method
File.link
(::path old_name, ::path new_name) -> 0
File.link(path, new_path) → 0
file.c
View on GitHub ↗
static VALUE
rb_file_s_link(VALUE klass, VALUE from, VALUE to)
{
FilePathValue(from);
FilePathValue(to);
from = rb_str_encode_ospath(from);
to = rb_str_encode_ospath(to);
if (link(StringValueCStr(from), StringValueCStr(to)) < 0) {
sys_fail2(from, to);
}
return INT2FIX(0);
}
Not available on some systems.
Creates a new entry at new_path for the existing entry at path using a hard link:
Example
File.write('doc/t.tmp', 'foo') File.link('doc/t.tmp', 'lib/u.tmp') File.read('lib/u.tmp') # => "foo" File.write('lib/u.tmp', 'bar') File.read('doc/t.tmp') # => "bar" File.delete('doc/t.tmp') File.read('lib/u.tmp') # => "bar" File.delete('lib/u.tmp')
Raises an exception if the entry at new_path exists.