Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
class method
Dir.home
(?::string? user) -> ::String
Dir.home(user_name = nil) → dirpath
dir.c
View on GitHub ↗
static VALUE
dir_s_home(int argc, VALUE *argv, VALUE obj)
{
VALUE user;
const char *u = 0;
rb_check_arity(argc, 0, 1);
user = (argc > 0) ? argv[0] : Qnil;
if (!NIL_P(user)) {
StringValue(user);
rb_must_asciicompat(user);
u = StringValueCStr(user);
if (*u) {
return rb_home_dir_of(user, rb_str_new(0, 0));
}
}
return rb_default_home_dir(rb_str_new(0, 0));
}
Returns the home directory path of the user specified with user_name if it is not nil, or the current login user:
Example
Dir.home # => "/home/me" Dir.home('root') # => "/root"
Raises ArgumentError if user_name is not a user name.