Calls the given block with each element, converting multiple values from yield to an array; returns self
:
a = [] (1..4).each_entry {|element| a.push(element) } # => 1..4 a # => [1, 2, 3, 4] a = [] h = {foo: 0, bar: 1, baz:2} h.each_entry {|element| a.push(element) } # => {:foo=>0, :bar=>1, :baz=>2} a # => [[:foo, 0], [:bar, 1], [:baz, 2]] class Foo include Enumerable def each yield 1 yield 1, 2 yield end end Foo.new.each_entry {|yielded| p yielded }
Output:
1 [1, 2] nil
With no block given, returns an Enumerator
.
Returns the last Error
of the current executing Thread
or nil if none
Sets the last Error
of the current executing Thread
to error
Arguments obj
and opts
here are the same as arguments obj
and opts
in JSON.generate
.
By default, generates JSON data without checking for circular references in obj
(option max_nesting
set to false
, disabled).
Raises an exception if obj
contains circular references:
a = []; b = []; a.push(b); b.push(a) # Raises SystemStackError (stack level too deep): JSON.fast_generate(a)
Turns FIPS mode on or off. Turning on FIPS mode will obviously only have an effect for FIPS-capable installations of the OpenSSL
library. Trying to do so otherwise will result in an error.
OpenSSL.fips_mode = true # turn FIPS mode on OpenSSL.fips_mode = false # and off again
Returns true
if the named file is writable by the real user and group id of this process. See access(3).
Note that some OS-level security features may cause this to return true even though the file is not writable by the real user/group.
If file_name is writable by others, returns an integer representing the file permission bits of file_name. Returns nil
otherwise. The meaning of the bits is platform dependent; on Unix systems, see stat(2)
.
file_name can be an IO
object.
File.world_writable?("/tmp") #=> 511 m = File.world_writable?("/tmp") sprintf("%o", m) #=> "777"
Returns information for heaps in the GC.
If the first optional argument, heap_name
, is passed in and not nil
, it returns a Hash
containing information about the particular heap. Otherwise, it will return a Hash
with heap names as keys and a Hash
containing information about the heap as values.
If the second optional argument, hash_or_key
, is given as Hash
, it will be overwritten and returned. This is intended to avoid the probe effect.
If both optional arguments are passed in and the second optional argument is a symbol, it will return a Numeric
of the value for the particular heap.
On CRuby, heap_name
is of the type Integer
but may be of type String
on other implementations.
The contents of the hash are implementation specific and may change in the future without notice.
If the optional argument, hash, is given, it is overwritten and returned.
This method is only expected to work on CRuby.
The hash includes the following keys about the internal information in the GC:
The slot size of the heap in bytes.
The number of pages that can be allocated without triggering a new garbage collection cycle.
The number of pages in the eden heap.
The total number of slots in all of the pages in the eden heap.
The number of pages in the tomb heap. The tomb heap only contains pages that do not have any live objects.
The total number of slots in all of the pages in the tomb heap.
The total number of pages that have been allocated in the heap.
The total number of pages that have been freed and released back to the system in the heap.
The number of times major garbage collection cycles this heap has forced to start due to running out of free slots.
The number of times this heap has forced incremental marking to complete due to running out of pooled slots.
Try to activate a gem containing path
. Returns true if activation succeeded or wasn’t needed because it was already activated. Returns false if it can’t find the path in a gem.
Adds a post-build hook that will be passed an Gem::Installer
instance when Gem::Installer#install
is called. The hook is called after the gem has been extracted and extensions have been built but before the executables or gemspec has been written. If the hook returns false
then the gem’s files will be removed and the install will be aborted.
Adds a post-installs hook that will be passed a Gem::DependencyInstaller
and a list of installed specifications when Gem::DependencyInstaller#install
is complete
Adds a hook that will get run after Gem::Specification.reset
is run.
Adds a pre-install hook that will be passed an Gem::Installer
instance when Gem::Installer#install
is called. If the hook returns false
then the install will be aborted.
Adds a pre-uninstall hook that will be passed an Gem::Uninstaller
instance and the spec that will be uninstalled when Gem::Uninstaller#uninstall
is called
Safely write a file in binary mode on all platforms.
Is this platform Solaris?
The path to standard location of the user’s state file.
The path to standard location of the user’s state directory.
Creates hard links; returns nil
.
Arguments src
and dest
should be interpretable as paths.
If src
is the path to a file and dest
does not exist, creates a hard link at dest
pointing to src
:
FileUtils.touch('src0.txt') File.exist?('dest0.txt') # => false FileUtils.link_entry('src0.txt', 'dest0.txt') File.file?('dest0.txt') # => true
If src
is the path to a directory and dest
does not exist, recursively creates hard links at dest
pointing to paths in src
:
FileUtils.mkdir_p(['src1/dir0', 'src1/dir1']) src_file_paths = [ 'src1/dir0/t0.txt', 'src1/dir0/t1.txt', 'src1/dir1/t2.txt', 'src1/dir1/t3.txt', ] FileUtils.touch(src_file_paths) File.directory?('dest1') # => true FileUtils.link_entry('src1', 'dest1') File.file?('dest1/dir0/t0.txt') # => true File.file?('dest1/dir0/t1.txt') # => true File.file?('dest1/dir1/t2.txt') # => true File.file?('dest1/dir1/t3.txt') # => true
Keyword arguments:
dereference_root: true
- dereferences src
if it is a symbolic link.
remove_destination: true
- removes dest
before creating links.
Raises an exception if dest
is the path to an existing file or directory and keyword argument remove_destination: true
is not given.
Related: FileUtils.ln
(has different options).
Creates hard links; returns nil
.
Arguments src
and dest
should be interpretable as paths.
If src
is the path to a file and dest
does not exist, creates a hard link at dest
pointing to src
:
FileUtils.touch('src0.txt') File.exist?('dest0.txt') # => false FileUtils.link_entry('src0.txt', 'dest0.txt') File.file?('dest0.txt') # => true
If src
is the path to a directory and dest
does not exist, recursively creates hard links at dest
pointing to paths in src
:
FileUtils.mkdir_p(['src1/dir0', 'src1/dir1']) src_file_paths = [ 'src1/dir0/t0.txt', 'src1/dir0/t1.txt', 'src1/dir1/t2.txt', 'src1/dir1/t3.txt', ] FileUtils.touch(src_file_paths) File.directory?('dest1') # => true FileUtils.link_entry('src1', 'dest1') File.file?('dest1/dir0/t0.txt') # => true File.file?('dest1/dir0/t1.txt') # => true File.file?('dest1/dir1/t2.txt') # => true File.file?('dest1/dir1/t3.txt') # => true
Keyword arguments:
dereference_root: true
- dereferences src
if it is a symbolic link.
remove_destination: true
- removes dest
before creating links.
Raises an exception if dest
is the path to an existing file or directory and keyword argument remove_destination: true
is not given.
Related: FileUtils.ln
(has different options).
Recursively copies files from src
to dest
.
Arguments src
and dest
should be interpretable as paths.
If src
is the path to a file, copies src
to dest
:
FileUtils.touch('src0.txt') File.exist?('dest0.txt') # => false FileUtils.copy_entry('src0.txt', 'dest0.txt') File.file?('dest0.txt') # => true
If src
is a directory, recursively copies src
to dest
:
tree('src1') # => src1 # |-- dir0 # | |-- src0.txt # | `-- src1.txt # `-- dir1 # |-- src2.txt # `-- src3.txt FileUtils.copy_entry('src1', 'dest1') tree('dest1') # => dest1 # |-- dir0 # | |-- src0.txt # | `-- src1.txt # `-- dir1 # |-- src2.txt # `-- src3.txt
The recursive copying preserves file types for regular files, directories, and symbolic links; other file types (FIFO streams, device files, etc.) are not supported.
Keyword arguments:
dereference_root: true
- if src
is a symbolic link, follows the link.
preserve: true
- preserves file times.
remove_destination: true
- removes dest
before copying files.
Related: methods for copying.
Recursively copies files from src
to dest
.
Arguments src
and dest
should be interpretable as paths.
If src
is the path to a file, copies src
to dest
:
FileUtils.touch('src0.txt') File.exist?('dest0.txt') # => false FileUtils.copy_entry('src0.txt', 'dest0.txt') File.file?('dest0.txt') # => true
If src
is a directory, recursively copies src
to dest
:
tree('src1') # => src1 # |-- dir0 # | |-- src0.txt # | `-- src1.txt # `-- dir1 # |-- src2.txt # `-- src3.txt FileUtils.copy_entry('src1', 'dest1') tree('dest1') # => dest1 # |-- dir0 # | |-- src0.txt # | `-- src1.txt # `-- dir1 # |-- src2.txt # `-- src3.txt
The recursive copying preserves file types for regular files, directories, and symbolic links; other file types (FIFO streams, device files, etc.) are not supported.
Keyword arguments:
dereference_root: true
- if src
is a symbolic link, follows the link.
preserve: true
- preserves file times.
remove_destination: true
- removes dest
before copying files.
Related: methods for copying.
Removes the entry given by path
, which should be the entry for a regular file, a symbolic link, or a directory.
Argument path
should be interpretable as a path.
Optional argument force
specifies whether to ignore raised exceptions of StandardError
and its descendants.
Related: FileUtils.remove_entry_secure
.