Equivalent to ($_.dup).chop!
, except nil
is never returned. See String#chop!
. Available only when -p/-n command line option specified.
Equivalent to $_ = $_.chomp(string)
. See String#chomp
. Available only when -p/-n command line option specified.
Deprecated. Use block_given? instead.
Returns a new array with the results of running block once for every element in enum.
If no block is given, an enumerator is returned instead.
(1..4).map { |i| i*i } #=> [1, 4, 9, 16] (1..4).collect { "cat" } #=> ["cat", "cat", "cat", "cat"]
Returns the object in enum with the maximum value. The first form assumes all objects implement <=>
; the second uses the block to return a <=> b.
a = %w(albatross dog horse) a.max #=> "horse" a.max { |a, b| a.length <=> b.length } #=> "albatross"
If the n
argument is given, maximum n
elements are returned as an array, sorted in descending order.
a = %w[albatross dog horse] a.max(2) #=> ["horse", "dog"] a.max(2) {|a, b| a.length <=> b.length } #=> ["albatross", "horse"] [5, 1, 3, 4, 2].max(3) #=> [5, 4, 3]
Enumerates over the items, chunking them together based on the return value of the block.
Consecutive elements which return the same block value are chunked together.
For example, consecutive even numbers and odd numbers can be chunked as follows.
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5].chunk { |n| n.even? }.each { |even, ary| p [even, ary] } #=> [false, [3, 1]] # [true, [4]] # [false, [1, 5, 9]] # [true, [2, 6]] # [false, [5, 3, 5]]
This method is especially useful for sorted series of elements. The following example counts words for each initial letter.
open("/usr/share/dict/words", "r:iso-8859-1") { |f| f.chunk { |line| line.upcase.ord }.each { |ch, lines| p [ch.chr, lines.length] } } #=> ["\n", 1] # ["A", 1327] # ["B", 1372] # ["C", 1507] # ["D", 791] # ...
The following key values have special meaning:
nil
and :_separator
specifies that the elements should be dropped.
:_alone
specifies that the element should be chunked by itself.
Any other symbols that begin with an underscore will raise an error:
items.chunk { |item| :_underscore } #=> RuntimeError: symbols beginning with an underscore are reserved
nil
and :_separator
can be used to ignore some elements.
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 }
:_alone
can be used to force items into their own chunk. For example, you can put lines that contain a URL by themselves, and chunk the rest of the lines together, like this:
pattern = /http/ open(filename) { |f| f.chunk { |line| line =~ pattern ? :_alone : true }.each { |key, lines| pp lines } }
If no block is given, an enumerator to ‘chunk` is returned instead.
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]
Computes the arctangent of decimal
to the specified number of digits of precision, numeric
.
If decimal
is NaN, returns NaN.
BigMath.atan(BigDecimal('-1'), 16).to_s #=> "-0.785398163397448309615660845819878471907514682065e0"
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)
Checks the status of the child process specified by pid
. Returns nil
if the process is still alive.
If the process is not alive, and raise
was true, a PTY::ChildExited
exception will be raised. Otherwise it will return a Process::Status
instance.
pid
The process id of the process to check
raise
If true
and the process identified by pid
is no longer alive a PTY::ChildExited
is raised.
Returns the log priority mask in effect. The mask is not reset by opening or closing syslog.
Sets the log priority mask. A method LOG_UPTO is defined to make it easier to set mask values. Example:
Syslog.mask = Syslog::LOG_UPTO(Syslog::LOG_ERR)
Alternatively, specific priorities can be selected and added together using binary OR. Example:
Syslog.mask = Syslog::LOG_MASK(Syslog::LOG_ERR) | Syslog::LOG_MASK(Syslog::LOG_CRIT)
The priority mask persists through calls to open() and close().
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 the named file is a character device.
file_name can be an IO
object.
Returns a Hash
containing information about the GC
.
The hash includes information about internal statistics about GC
such as:
{ :count=>0, :heap_allocated_pages=>24, :heap_sorted_length=>24, :heap_allocatable_pages=>0, :heap_available_slots=>9783, :heap_live_slots=>7713, :heap_free_slots=>2070, :heap_final_slots=>0, :heap_marked_slots=>0, :heap_eden_pages=>24, :heap_tomb_pages=>0, :total_allocated_pages=>24, :total_freed_pages=>0, :total_allocated_objects=>7796, :total_freed_objects=>83, :malloc_increase_bytes=>2389312, :malloc_increase_bytes_limit=>16777216, :minor_gc_count=>0, :major_gc_count=>0, :remembered_wb_unprotected_objects=>0, :remembered_wb_unprotected_objects_limit=>0, :old_objects=>0, :old_objects_limit=>0, :oldmalloc_increase_bytes=>2389760, :oldmalloc_increase_bytes_limit=>16777216 }
The contents of the hash are implementation specific and may be changed in the future.
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 C Ruby.
Returns true if new
is newer than all old_list
. Non-existent files are older than any file.
FileUtils.uptodate?('hello.o', %w(hello.c hello.h)) or \ system 'make hello.o'
Returns true if new
is newer than all old_list
. Non-existent files are older than any file.
FileUtils.uptodate?('hello.o', %w(hello.c hello.h)) or \ system 'make hello.o'