Returns an array containing the items in enum.
(1..7).to_a #=> [1, 2, 3, 4, 5, 6, 7] { 'a'=>1, 'b'=>2, 'c'=>3 }.to_a #=> [["a", 1], ["b", 2], ["c", 3]] require 'prime' Prime.entries 10 #=> [2, 3, 5, 7]
Returns the result of interpreting enum as a list of [key, value]
pairs.
%i[hello world].each_with_index.to_h # => {:hello => 0, :world => 1}
If a block is given, the results of the block on each element of the enum will be used as pairs.
(1..5).to_h {|x| [x, x ** 2]} #=> {1=>1, 2=>4, 3=>9, 4=>16, 5=>25}
Returns an array containing the items in enum sorted.
Comparisons for the sort will be done using the items’ own <=>
operator or using an optional code block.
The block must implement a comparison between a
and b
and return an integer less than 0 when b
follows a
, 0
when a
and b
are equivalent, or an integer greater than 0 when a
follows b
.
The result is not guaranteed to be stable. When the comparison of two elements returns 0
, the order of the elements is unpredictable.
%w(rhea kea flea).sort #=> ["flea", "kea", "rhea"] (1..10).sort { |a, b| b <=> a } #=> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
See also Enumerable#sort_by
. It implements a Schwartzian transform which is useful when key computation or comparison is expensive.
Returns an array of every element in enum for which Pattern === element
. If the optional block is supplied, each matching element is passed to it, and the block’s result is stored in the output array.
(1..100).grep 38..44 #=> [38, 39, 40, 41, 42, 43, 44] c = IO.constants c.grep(/SEEK/) #=> [:SEEK_SET, :SEEK_CUR, :SEEK_END] res = c.grep(/SEEK/) { |v| IO.const_get(v) } res #=> [0, 1, 2]
Inverted version of Enumerable#grep
. Returns an array of every element in enum for which not Pattern === element
.
(1..10).grep_v 2..5 #=> [1, 6, 7, 8, 9, 10] res =(1..10).grep_v(2..5) { |v| v * 2 } res #=> [2, 12, 14, 16, 18, 20]
Returns an array for all elements of enum
for which the given block
returns false
.
If no block is given, an Enumerator
is returned instead.
(1..10).reject { |i| i % 3 == 0 } #=> [1, 2, 4, 5, 7, 8, 10] [1, 2, 3, 4, 5].reject { |num| num.even? } #=> [1, 3, 5]
See also Enumerable#find_all
.
Combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator.
The inject and reduce methods are aliases. There is no performance benefit to either.
If you specify a block, then for each element in enum the block is passed an accumulator value (memo) and the element. If you specify a symbol instead, then each element in the collection will be passed to the named method of memo. In either case, the result becomes the new value for memo. At the end of the iteration, the final value of memo is the return value for the method.
If you do not explicitly specify an initial value for memo, then the first element of collection is used as the initial value of memo.
# Sum some numbers (5..10).reduce(:+) #=> 45 # Same using a block and inject (5..10).inject { |sum, n| sum + n } #=> 45 # Multiply some numbers (5..10).reduce(1, :*) #=> 151200 # Same using a block (5..10).inject(1) { |product, n| product * n } #=> 151200 # find the longest word longest = %w{ cat sheep bear }.inject do |memo, word| memo.length > word.length ? memo : word end longest #=> "sheep"
Returns the first element, or the first n
elements, of the enumerable. If the enumerable is empty, the first form returns nil
, and the second form returns an empty array.
%w[foo bar baz].first #=> "foo" %w[foo bar baz].first(2) #=> ["foo", "bar"] %w[foo bar baz].first(10) #=> ["foo", "bar", "baz"] [].first #=> nil [].first(10) #=> []
Enables coverage measurement.
Returns a hash that contains filename as key and coverage array as value. If clear
is true, it clears the counters to zero. If stop
is true, it disables coverage measurement.
Resets the process of reading the /etc/group file, so that the next call to ::getgrent
will return the first entry again.
Ends the process of scanning through the /etc/group file begun by ::getgrent
, and closes the file.
Returns an entry from the /etc/group file.
The first time it is called it opens the file and returns the first entry; each successive call returns the next entry, or nil
if the end of the file has been reached.
To close the file when processing is complete, call ::endgrent
.
Each entry is returned as a Group
struct
Returns system temporary directory; typically “/tmp”.
Returns system configuration variable using confstr().
name should be a constant under Etc
which begins with CS_
.
The return value is a string or nil. nil means no configuration-defined value. (confstr() returns 0 but errno is not set.)
Etc.confstr(Etc::CS_PATH) #=> "/bin:/usr/bin" # GNU/Linux Etc.confstr(Etc::CS_GNU_LIBC_VERSION) #=> "glibc 2.18" Etc.confstr(Etc::CS_GNU_LIBPTHREAD_VERSION) #=> "NPTL 2.18"
Returns the number of online processors.
The result is intended as the number of processes to use all available processors.
This method is implemented using:
sched_getaffinity(): Linux
sysconf(_SC_NPROCESSORS_ONLN): GNU/Linux, NetBSD, FreeBSD, OpenBSD, DragonFly BSD, OpenIndiana, Mac OS X, AIX
Example:
require 'etc' p Etc.nprocessors #=> 4
The result might be smaller number than physical cpus especially when ruby process is bound to specific cpus. This is intended for getting better parallel processing.
Example: (Linux)
linux$ taskset 0x3 ./ruby -retc -e "p Etc.nprocessors" #=> 2
Change the size of the memory allocated at the memory location addr
to size
bytes. Returns the memory address of the reallocated memory, which may be different than the address passed in.
Free the memory at address addr
Convert str
to ISO-2022-JP
Convert str
to ISO-2022-JP
Convert str
to EUC-JP
Convert str
to EUC-JP
Convert str
to Shift_JIS
Convert str
to Shift_JIS