Equivalent to $_ = $_.chomp(string)
. See String#chomp
. Available only when -p/-n command line option specified.
Deprecated. Use block_given? instead.
Returns an array of objects returned by the block.
With a block given, calls the block with successive elements; returns an array of the objects returned by the block:
(0..4).map {|i| i*i } # => [0, 1, 4, 9, 16] {foo: 0, bar: 1, baz: 2}.map {|key, value| value*2} # => [0, 2, 4]
With no block given, returns an Enumerator
.
Returns the element with the maximum element according to a given criterion. The ordering of equal elements is indeterminate and may be unstable.
With no argument and no block, returns the maximum element, using the elements’ own method <=>
for comparison:
(1..4).max # => 4 (-4..-1).max # => -1 %w[d c b a].max # => "d" {foo: 0, bar: 1, baz: 2}.max # => [:foo, 0] [].max # => nil
With positive integer argument n
given, and no block, returns an array containing the first n
maximum elements that exist:
(1..4).max(2) # => [4, 3] (-4..-1).max(2) # => [-1, -2] %w[d c b a].max(2) # => ["d", "c"] {foo: 0, bar: 1, baz: 2}.max(2) # => [[:foo, 0], [:baz, 2]] [].max(2) # => []
With a block given, the block determines the maximum elements. The block is called with two elements a
and b
, and must return:
A negative integer if a < b
.
Zero if a == b
.
A positive integer if a > b
.
With a block given and no argument, returns the maximum element as determined by the block:
%w[xxx x xxxx xx].max {|a, b| a.size <=> b.size } # => "xxxx" h = {foo: 0, bar: 1, baz: 2} h.max {|pair1, pair2| pair1[1] <=> pair2[1] } # => [:baz, 2] [].max {|a, b| a <=> b } # => nil
With a block given and positive integer argument n
given, returns an array containing the first n
maximum elements that exist, as determined by the block.
%w[xxx x xxxx xx].max(2) {|a, b| a.size <=> b.size } # => ["xxxx", "xxx"] h = {foo: 0, bar: 1, baz: 2} h.max(2) {|pair1, pair2| pair1[1] <=> pair2[1] } # => [[:baz, 2], [:bar, 1]] [].max(2) {|a, b| a <=> b } # => []
Each element in the returned enumerator is a 2-element array consisting of:
A value returned by the block.
An array (“chunk”) containing the element for which that value was returned, and all following elements for which the block returned the same value:
So that:
Each block return value that is different from its predecessor begins a new chunk.
Each block return value that is the same as its predecessor continues the same chunk.
Example:
e = (0..10).chunk {|i| (i / 3).floor } # => #<Enumerator: ...> # The enumerator elements. e.next # => [0, [0, 1, 2]] e.next # => [1, [3, 4, 5]] e.next # => [2, [6, 7, 8]] e.next # => [3, [9, 10]]
Method chunk
is especially useful for an enumerable that is already sorted. This example counts words for each initial letter in a large array of words:
# Get sorted words from a web page. url = 'https://raw.githubusercontent.com/eneko/data-repository/master/data/words.txt' words = URI::open(url).readlines # Make chunks, one for each letter. e = words.chunk {|word| word.upcase[0] } # => #<Enumerator: ...> # Display 'A' through 'F'. e.each {|c, words| p [c, words.length]; break if c == 'F' }
Output:
["A", 17096] ["B", 11070] ["C", 19901] ["D", 10896] ["E", 8736] ["F", 6860]
You can use the special symbol :_alone
to force an element into its own separate chuck:
a = [0, 0, 1, 1] e = a.chunk{|i| i.even? ? :_alone : true } e.to_a # => [[:_alone, [0]], [:_alone, [0]], [true, [1, 1]]]
For example, you can put each line that contains a URL into its own chunk:
pattern = /http/ open(filename) { |f| f.chunk { |line| line =~ pattern ? :_alone : true }.each { |key, lines| pp lines } }
You can use the special symbol :_separator
or nil
to force an element to be ignored (not included in any chunk):
a = [0, 0, -1, 1, 1] e = a.chunk{|i| i < 0 ? :_separator : true } e.to_a # => [[true, [0, 0]], [true, [1, 1]]]
Note that the separator does end the chunk:
a = [0, 0, -1, 1, -1, 1] e = a.chunk{|i| i < 0 ? :_separator : true } e.to_a # => [[true, [0, 0]], [true, [1]], [true, [1]]]
For example, the sequence of hyphens in svn log can be eliminated as follows:
sep = "-"*72 + "\n" IO.popen("svn log README") { |f| f.chunk { |line| line != sep || nil }.each { |_, lines| pp lines } } #=> ["r20018 | knu | 2008-10-29 13:20:42 +0900 (Wed, 29 Oct 2008) | 2 lines\n", # "\n", # "* README, README.ja: Update the portability section.\n", # "\n"] # ["r16725 | knu | 2008-05-31 23:34:23 +0900 (Sat, 31 May 2008) | 2 lines\n", # "\n", # "* README, README.ja: Add a note about default C flags.\n", # "\n"] # ...
Paragraphs separated by empty lines can be parsed as follows:
File.foreach("README").chunk { |line| /\A\s*\z/ !~ line || nil }.each { |_, lines| pp lines }
Returns an enumerator object generated from this enumerator and given enumerables.
e = (1..3).chain([4, 5]) e.to_a #=> [1, 2, 3, 4, 5]
Returns a list of the supported category symbols.
Returns the state of the coverage measurement.
Allocate size
bytes of memory and return the integer memory address for the allocated memory.
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)
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
Returns true
if filepath
points to a character device, false
otherwise.
File.chardev?($stdin) # => true File.chardev?('t.txt') # => 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.
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
Retrieve the PathSupport
object that RubyGems uses to lookup files.
Initialize the filesystem paths to use from env
. env
is a hash-like object (typically ENV
) that is queried for ‘GEM_HOME’, ‘GEM_PATH’, and ‘GEM_SPEC_CACHE’ Keys for the env
hash should be Strings, and values of the hash should be Strings or nil
.
Set
array of platforms this RubyGems supports (primarily for testing).
Array
of platforms this RubyGems supports.