Returns true
if the given string (or symbol) exists as a thread-local variable.
me = Thread.current me.thread_variable_set(:oliver, "a") me.thread_variable?(:oliver) #=> true me.thread_variable?(:stanley) #=> false
Note that these are not fiber local variables. Please see Thread#[]
and Thread#thread_variable_get
for more details.
Return the name at the definition of the method being called
Return the called name of the method being called
Returns an array of the names of global variables. This includes special regexp global variables such as $~
and $+
, but does not include the numbered regexp global variables ($1
, $2
, etc.).
global_variables.grep /std/ #=> [:$stdin, :$stdout, :$stderr]
Returns the names of the current local variables.
fred = 1 for i in 1..10 # ... end local_variables #=> [:fred, :i]
Calls the block with successive elements as long as the block returns a truthy value; returns an array of all elements up to that point:
(1..4).take_while{|i| i < 3 } # => [1, 2] h = {foo: 0, bar: 1, baz: 2} h.take_while{|element| key, value = *element; value < 2 } # => [[:foo, 0], [:bar, 1]]
With no block given, returns an Enumerator
.
Calls the block with successive elements as long as the block returns a truthy value; returns an array of all elements after that point:
(1..4).drop_while{|i| i < 3 } # => [3, 4] h = {foo: 0, bar: 1, baz: 2} a = h.drop_while{|element| key, value = *element; value < 2 } a # => [[:baz, 2]]
With no block given, returns an Enumerator
.
Returns a hash that contains filename as key and coverage array as value. This is the same as ‘Coverage.result(stop: false, clear: false)`.
{ "file.rb" => [1, 2, nil], ... }
Dump the contents of the ruby heap as JSON
.
output argument is the same as for dump
.
full must be a boolean. If true, all heap slots are dumped including the empty ones (T_NONE
).
since must be a non-negative integer or nil
.
If since is a positive integer, only objects of that generation and newer generations are dumped. The current generation can be accessed using GC::count
. Objects that were allocated without object allocation tracing enabled are ignored. See ::trace_object_allocations
for more information and examples.
If since is omitted or is nil
, all objects are dumped.
shapes must be a boolean or a non-negative integer.
If shapes is a positive integer, only shapes newer than the provided shape id are dumped. The current shape_id can be accessed using RubyVM.stat(:next_shape_id)
.
If shapes is false
, no shapes are dumped.
To only dump objects allocated past a certain point you can combine since and shapes:
ObjectSpace.trace_object_allocations GC.start gc_generation = GC.count shape_generation = RubyVM.stat(:next_shape_id) call_method_to_instrument ObjectSpace.dump_all(since: gc_generation, shapes: shape_generation)
This method is only expected to work with C Ruby. This is an experimental method and is subject to change. In particular, the function signature and output format are not guaranteed to be compatible in future versions of ruby.
Dump the contents of the ruby shape tree as JSON
.
output argument is the same as for dump
.
If since is a positive integer, only shapes newer than the provided shape id are dumped. The current shape_id can be accessed using RubyVM.stat(:next_shape_id)
.
This method is only expected to work with C Ruby. This is an experimental method and is subject to change. In particular, the function signature and output format are not guaranteed to be compatible in future versions of ruby.
Parse a file at filename
. Returns the Psych::Nodes::Document
.
Raises a Psych::SyntaxError
when a YAML
syntax error is detected.
Safely dump Ruby object o
to a YAML
string. Optional options
may be passed in to control the output format. If an IO
object is passed in, the YAML
will be dumped to that IO
object. By default, only the following classes are allowed to be serialized:
Arbitrary classes can be allowed by adding those classes to the permitted_classes
keyword argument. They are additive. For example, to allow Date
serialization:
Psych.safe_dump(yaml, permitted_classes: [Date])
Now the Date
class can be dumped in addition to the classes listed above.
A Psych::DisallowedClass
exception will be raised if the object contains a class that isn’t in the permitted_classes
list.
Currently supported options are:
:indentation
Number of space characters used to indent. Acceptable value should be in 0..9
range, otherwise option is ignored.
Default: 2
.
:line_width
Max character to wrap line at.
Default: 0
(meaning “wrap at 81”).
:canonical
Write “canonical” YAML
form (very verbose, yet strictly formal).
Default: false
.
:header
Write %YAML [version]
at the beginning of document.
Default: false
.
:stringify_names
Dump symbol keys in Hash
objects as string.
Default: false
.
Example:
# Dump an array, get back a YAML string Psych.safe_dump(['a', 'b']) # => "---\n- a\n- b\n" # Dump an array to an IO object Psych.safe_dump(['a', 'b'], StringIO.new) # => #<StringIO:0x000001009d0890> # Dump an array with indentation set Psych.safe_dump(['a', ['b']], indentation: 3) # => "---\n- a\n- - b\n" # Dump an array to an IO with indentation set Psych.safe_dump(['a', ['b']], StringIO.new, indentation: 3) # Dump hash with symbol keys as string Psych.dump({a: "b"}, stringify_names: true) # => "---\na: b\n"
Dump a list of objects as separate documents to a document stream.
Example:
Psych.dump_stream("foo\n ", {}) # => "--- ! \"foo\\n \"\n--- {}\n"
Loads the document contained in filename
. Returns the yaml contained in filename
as a Ruby object, or if the file is empty, it returns the specified fallback
return value, which defaults to false
. See load for options.
Combine two Adler-32 check values in to one. adler1
is the first Adler-32 value, adler2
is the second Adler-32 value. len2
is the length of the string used to generate adler2
.
Returns the table for calculating CRC checksum as an array.
Returns true
if the named file is readable 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 readable by the real user/group.
If file_name is readable 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_readable?("/etc/passwd") #=> 420 m = File.world_readable?("/etc/passwd") sprintf("%o", m) #=> "644"
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 true
if the named file is executable by the real user and group id of this process. See access(3).
Windows does not support execute permissions separately from read permissions. On Windows, a file is only considered executable if it ends in .bat, .cmd, .com, or .exe.
Note that some OS-level security features may cause this to return true even though the file is not executable by the real user/group.
Reset the dir
and path
values. The next time dir
or path
is requested, the values will be calculated from scratch. This is mainly used by the unit tests to provide test isolation.
Returns a list of paths matching glob
that can be used by a gem to pick up features from other gems. For example:
Gem.find_files('rdoc/discover').each do |path| load path end
if check_load_path
is true (the default), then find_files
also searches $LOAD_PATH for files as well as gems.
Note that find_files
will return all files even if they are from different versions of the same gem. See also find_latest_files