Returns an array of all non-nil
elements:
a = [nil, 0, nil, 'a', false, nil, false, nil, 'a', nil, 0, nil] a.compact # => [0, "a", false, false, "a", 0]
Returns a list of the supported category symbols.
Start/resume the coverage measurement.
Caveat: Currently, only process-global coverage measurement is supported. You cannot measure per-thread coverage. If your process has multiple thread, using Coverage.resume
/suspend to capture code coverage executed from only a limited code block, may yield misleading results.
Returns the state of the coverage measurement.
Provides a convenient Ruby iterator which executes a block for each entry in the /etc/passwd
file.
The code block is passed an Passwd
struct.
See ::getpwent
above for details.
Example:
require 'etc' Etc.passwd {|u| puts u.name + " = " + u.gecos }
Returns the Ruby objects created by parsing the given source
.
Argument source
contains the String to be parsed.
Argument opts
, if given, contains a Hash of options for the parsing. See Parsing Options.
When source
is a JSON array, returns a Ruby Array:
source = '["foo", 1.0, true, false, null]' ruby = JSON.parse(source) ruby # => ["foo", 1.0, true, false, nil] ruby.class # => Array
When source
is a JSON object, returns a Ruby Hash:
source = '{"a": "foo", "b": 1.0, "c": true, "d": false, "e": null}' ruby = JSON.parse(source) ruby # => {"a"=>"foo", "b"=>1.0, "c"=>true, "d"=>false, "e"=>nil} ruby.class # => Hash
For examples of parsing for all JSON data types, see Parsing JSON.
Parses nested JSON
objects:
source = <<-EOT { "name": "Dave", "age" :40, "hats": [ "Cattleman's", "Panama", "Tophat" ] } EOT ruby = JSON.parse(source) ruby # => {"name"=>"Dave", "age"=>40, "hats"=>["Cattleman's", "Panama", "Tophat"]}
Raises an exception if source
is not valid JSON:
# Raises JSON::ParserError (783: unexpected token at ''): JSON.parse('')
Calls
parse(source, opts)
with source
and possibly modified opts
.
Differences from JSON.parse
:
Option max_nesting
, if not provided, defaults to false
, which disables checking for nesting depth.
Option allow_nan
, if not provided, defaults to true
.
Returns a String containing the generated JSON data.
See also JSON.fast_generate
, JSON.pretty_generate
.
Argument obj
is the Ruby object to be converted to JSON.
Argument opts
, if given, contains a Hash of options for the generation. See Generating Options.
When obj
is an Array, returns a String containing a JSON array:
obj = ["foo", 1.0, true, false, nil] json = JSON.generate(obj) json # => '["foo",1.0,true,false,null]'
When obj
is a Hash, returns a String containing a JSON object:
obj = {foo: 0, bar: 's', baz: :bat} json = JSON.generate(obj) json # => '{"foo":0,"bar":"s","baz":"bat"}'
For examples of generating from other Ruby objects, see Generating JSON from Other Objects.
Raises an exception if any formatting option is not a String.
Raises an exception if obj
contains circular references:
a = []; b = []; a.push(b); b.push(a) # Raises JSON::NestingError (nesting of 100 is too deep): JSON.generate(a)
Parse a YAML
string in yaml
. Returns the Psych::Nodes::Document
. filename
is used in the exception message if a Psych::SyntaxError
is raised.
Raises a Psych::SyntaxError
when a YAML
syntax error is detected.
Example:
Psych.parse("---\n - a\n - b") # => #<Psych::Nodes::Document:0x00> begin Psych.parse("--- `", filename: "file.txt") rescue Psych::SyntaxError => ex ex.file # => 'file.txt' ex.message # => "(file.txt): found character that cannot start any token" end
See Psych::Nodes
for more information about YAML
AST.
Returns a default parser
Compresses the given string
. Valid values of level are Zlib::NO_COMPRESSION
, Zlib::BEST_SPEED
, Zlib::BEST_COMPRESSION
, Zlib::DEFAULT_COMPRESSION
, or an integer from 0 to 9.
This method is almost equivalent to the following code:
def deflate(string, level) z = Zlib::Deflate.new(level) dst = z.deflate(string, Zlib::FINISH) z.close dst end
See also Zlib.inflate
Decompresses string
. Raises a Zlib::NeedDict
exception if a preset dictionary is needed for decompression.
This method is almost equivalent to the following code:
def inflate(string) zstream = Zlib::Inflate.new buf = zstream.inflate(string) zstream.finish zstream.close buf end
See also Zlib.deflate
Enables garbage collection, returning true
if garbage collection was previously disabled.
GC.disable #=> false GC.enable #=> true GC.enable #=> false
Returns a Hash
containing information about the GC.
The contents of the hash are implementation specific and may change in the future without notice.
The hash includes information about internal statistics about GC such as:
The total number of garbage collections ran since application start (count includes both minor and major garbage collections)
The total time spent in garbage collections (in milliseconds)
The total number of :heap_eden_pages
+ :heap_tomb_pages
The number of pages that can fit into the buffer that holds references to all pages
The total number of pages the application could allocate without additional GC
The total number of slots in all :heap_allocated_pages
The total number of slots which contain live objects
The total number of slots which do not contain live objects
The total number of slots with pending finalizers to be run
The total number of objects marked in the last GC
The total number of pages which contain at least one live slot
The total number of pages which do not contain any live slots
The cumulative number of pages allocated since application start
The cumulative number of pages freed since application start
The cumulative number of objects allocated since application start
The cumulative number of objects freed since application start
Amount of memory allocated on the heap for objects. Decreased by any GC
When :malloc_increase_bytes
crosses this limit, GC is triggered
The total number of minor garbage collections run since process start
The total number of major garbage collections run since process start
The total number of compactions run since process start
The total number of times the read barrier was triggered during compaction
The total number of objects compaction has moved
The total number of objects without write barriers
When :remembered_wb_unprotected_objects
crosses this limit, major GC is triggered
Number of live, old objects which have survived at least 3 garbage collections
When :old_objects
crosses this limit, major GC is triggered
Amount of memory allocated on the heap for objects. Decreased by major GC
When :old_malloc_increase_bytes
crosses this limit, major GC is triggered
If the optional argument, hash, is given, it is overwritten and returned. This is intended to avoid probe effect.
This method is only expected to work on CRuby.
Returns the time used to execute the given block as a Benchmark::Tms
object. Takes label
option.
require 'benchmark' n = 1000000 time = Benchmark.measure do n.times { a = "1" } end puts time
Generates:
0.220000 0.000000 0.220000 ( 0.227313)
Returns the elapsed real time used to execute the given block.
Returns the time used to execute the given block as a Benchmark::Tms
object. Takes label
option.
require 'benchmark' n = 1000000 time = Benchmark.measure do n.times { a = "1" } end puts time
Generates:
0.220000 0.000000 0.220000 ( 0.227313)
Returns the elapsed real time used to execute the given block.
The standard configuration object for gems.
Use the given configuration object (which implements the ConfigFile
protocol) as the standard configuration object.
A Zlib::Deflate.deflate
wrapper
Set
array of platforms this RubyGems supports (primarily for testing).
Array
of platforms this RubyGems supports.
Prints the amount of time the supplied block takes to run using the debug UI output.
Returns the currently set formatter. By default, it is set to DidYouMean::Formatter
.