Returns the hostname.
p Socket.gethostname #=> "hal"
Note that it is not guaranteed to be able to convert to IP address using gethostbyname, getaddrinfo, etc. If you need local IP address, use Socket.ip_address_list
.
Use Addrinfo.getaddrinfo
instead. This method is deprecated for the following reasons:
The 3rd element of the result is the address family of the first address. The address families of the rest of the addresses are not returned.
Uncommon address representation: 4/16-bytes binary string to represent IPv4/IPv6 address.
gethostbyname() may take a long time and it may block other threads. (GVL cannot be released since gethostbyname() is not thread safe.)
This method uses gethostbyname() function already removed from POSIX.
This method obtains the host information for hostname.
p Socket.gethostbyname("hal") #=> ["localhost", ["hal"], 2, "\x7F\x00\x00\x01"]
Use Addrinfo.getaddrinfo
instead. This method is deprecated for the following reasons:
The 3rd element of the result is the address family of the first address. The address families of the rest of the addresses are not returned.
gethostbyname() may take a long time and it may block other threads. (GVL cannot be released since gethostbyname() is not thread safe.)
This method uses gethostbyname() function already removed from POSIX.
This method lookups host information by hostname.
TCPSocket.gethostbyname("localhost") #=> ["localhost", ["hal"], 2, "127.0.0.1"]
How String Gem paths should be split. Overridable for esoteric platforms.
Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless dir_string
is given, in which case it will be used as the starting point. The given pathname may start with a “~
”, which expands to the process owner’s home directory (the environment variable HOME
must be set correctly). “~
user” expands to the named user’s home directory.
File.expand_path("~oracle/bin") #=> "/home/oracle/bin"
A simple example of using dir_string
is as follows.
File.expand_path("ruby", "/usr/bin") #=> "/usr/bin/ruby"
A more complex example which also resolves parent directory is as follows. Suppose we are in bin/mygem and want the absolute path of lib/mygem.rb.
File.expand_path("../../lib/mygem.rb", __FILE__) #=> ".../path/to/project/lib/mygem.rb"
So first it resolves the parent of __FILE__, that is bin/, then go to the parent, the root of the project and appends lib/mygem.rb
.
Program name to be emitted in error message and default banner, defaults to $0.
Returns the original name of the method.
class C def foo; end alias bar foo end C.instance_method(:bar).original_name # => :foo
Returns the original name of the method.
class C def foo; end alias bar foo end C.instance_method(:bar).original_name # => :foo
Renames the given file to the new name. Raises a SystemCallError
if the file cannot be renamed.
File.rename("afile", "afile.bak") #=> 0
Returns the real (absolute) pathname of pathname in the actual filesystem not containing symlinks or useless dots.
If dir_string is given, it is used as a base directory for interpreting relative pathname instead of the current directory.
All components of the pathname must exist when this method is called.
Returns the real (absolute) pathname of pathname in the actual filesystem. The real pathname doesn’t contain symlinks or useless dots.
If dir_string is given, it is used as a base directory for interpreting relative pathname instead of the current directory.
The last component of the real pathname can be nonexistent.
Returns the last component of the filename given in file_name (after first stripping trailing separators), which can be formed using both File::SEPARATOR
and File::ALT_SEPARATOR
as the separator when File::ALT_SEPARATOR
is not nil
. If suffix is given and present at the end of file_name, it is removed. If suffix is “.*”, any extension will be removed.
File.basename("/home/gumby/work/ruby.rb") #=> "ruby.rb" File.basename("/home/gumby/work/ruby.rb", ".rb") #=> "ruby" File.basename("/home/gumby/work/ruby.rb", ".*") #=> "ruby"
Returns all components of the filename given in file_name except the last one (after first stripping trailing separators). The filename can be formed using both File::SEPARATOR
and File::ALT_SEPARATOR
as the separator when File::ALT_SEPARATOR
is not nil
.
File.dirname("/home/gumby/work/ruby.rb") #=> "/home/gumby/work"
Returns the extension (the portion of file name in path
starting from the last period).
If path
is a dotfile, or starts with a period, then the starting dot is not dealt with the start of the extension.
An empty string will also be returned when the period is the last character in path
.
File.extname("test.rb") #=> ".rb" File.extname("a/b/d/test.rb") #=> ".rb" File.extname(".a/b/d/test.rb") #=> ".rb" File.extname("foo.") #=> "" File.extname("test") #=> "" File.extname(".profile") #=> "" File.extname(".profile.sh") #=> ".sh"
Returns the string representation of the path
File.path("/dev/null") #=> "/dev/null" File.path(Pathname.new("/tmp")) #=> "/tmp"
Returns the pathname used to create file as a string. Does not normalize the name.
The pathname may not point to the file corresponding to file. For instance, the pathname becomes void when the file has been moved or deleted.
This method raises IOError
for a file created using File::Constants::TMPFILE
because they don’t have a pathname.
File.new("testfile").path #=> "testfile" File.new("/tmp/../tmp/xxx", "w").path #=> "/tmp/../tmp/xxx"
Returns the list of name and aliases of the encoding.
Encoding::WINDOWS_31J.names #=> ["Windows-31J", "CP932", "csWindows31J"]