Returns the minimum value in self
, using method <=>
or a given block for comparison.
With no argument and no block given, returns the minimum-valued element of self
.
(1..4).min # => 1 ('a'..'d').min # => "a" (-4..-1).min # => -4
With non-negative integer argument n
given, and no block given, returns the n
minimum-valued elements of self
in an array:
(1..4).min(2) # => [1, 2] ('a'..'d').min(2) # => ["a", "b"] (-4..-1).min(2) # => [-4, -3] (1..4).min(50) # => [1, 2, 3, 4]
If a block is given, it is called:
First, with the first two element of self
.
Then, sequentially, with the so-far minimum value and the next element of self
.
To illustrate:
(1..4).min {|a, b| p [a, b]; a <=> b } # => 1
Output:
[2, 1] [3, 1] [4, 1]
With no argument and a block given, returns the return value of the last call to the block:
(1..4).min {|a, b| -(a <=> b) } # => 4
With non-negative integer argument n
given, and a block given, returns the return values of the last n
calls to the block in an array:
(1..4).min(2) {|a, b| -(a <=> b) } # => [4, 3] (1..4).min(50) {|a, b| -(a <=> b) } # => [4, 3, 2, 1]
Returns an empty array if n
is zero:
(1..4).min(0) # => [] (1..4).min(0) {|a, b| -(a <=> b) } # => []
Returns nil
or an empty array if:
The begin value of the range is larger than the end value:
(4..1).min # => nil (4..1).min(2) # => [] (4..1).min {|a, b| -(a <=> b) } # => nil (4..1).min(2) {|a, b| -(a <=> b) } # => []
The begin value of an exclusive range is equal to the end value:
(1...1).min # => nil (1...1).min(2) # => [] (1...1).min {|a, b| -(a <=> b) } # => nil (1...1).min(2) {|a, b| -(a <=> b) } # => []
Raises an exception if either:
self
is a beginless range: (..4)
.
A block is given and self
is an endless range.
Related: Range#max
, Range#minmax
.
Returns the maximum value in self
, using method <=>
or a given block for comparison.
With no argument and no block given, returns the maximum-valued element of self
.
(1..4).max # => 4 ('a'..'d').max # => "d" (-4..-1).max # => -1
With non-negative integer argument n
given, and no block given, returns the n
maximum-valued elements of self
in an array:
(1..4).max(2) # => [4, 3] ('a'..'d').max(2) # => ["d", "c"] (-4..-1).max(2) # => [-1, -2] (1..4).max(50) # => [4, 3, 2, 1]
If a block is given, it is called:
First, with the first two element of self
.
Then, sequentially, with the so-far maximum value and the next element of self
.
To illustrate:
(1..4).max {|a, b| p [a, b]; a <=> b } # => 4
Output:
[2, 1] [3, 2] [4, 3]
With no argument and a block given, returns the return value of the last call to the block:
(1..4).max {|a, b| -(a <=> b) } # => 1
With non-negative integer argument n
given, and a block given, returns the return values of the last n
calls to the block in an array:
(1..4).max(2) {|a, b| -(a <=> b) } # => [1, 2] (1..4).max(50) {|a, b| -(a <=> b) } # => [1, 2, 3, 4]
Returns an empty array if n
is zero:
(1..4).max(0) # => [] (1..4).max(0) {|a, b| -(a <=> b) } # => []
Returns nil
or an empty array if:
The begin value of the range is larger than the end value:
(4..1).max # => nil (4..1).max(2) # => [] (4..1).max {|a, b| -(a <=> b) } # => nil (4..1).max(2) {|a, b| -(a <=> b) } # => []
The begin value of an exclusive range is equal to the end value:
(1...1).max # => nil (1...1).max(2) # => [] (1...1).max {|a, b| -(a <=> b) } # => nil (1...1).max(2) {|a, b| -(a <=> b) } # => []
Raises an exception if either:
self
is a endless range: (1..)
.
A block is given and self
is a beginless range.
Related: Range#min
, Range#minmax
.
Returns the denominator (always positive).
Rational(7).denominator #=> 1 Rational(7, 1).denominator #=> 1 Rational(9, -4).denominator #=> 4 Rational(-2, -10).denominator #=> 5
Sets the data mode in self
to binary mode; see Data Mode.
Sets the scan pointer to the end of the string and clear matching data.
Puts ARGF
into binary mode. Once a stream is in binary mode, it cannot be reset to non-binary mode. This option has the following effects:
Newline conversion is disabled.
Encoding
conversion is disabled.
Content is treated as ASCII-8BIT.
Returns true if ARGF
is being read in binary mode; false otherwise. To enable binary mode use ARGF.binmode
.
For example:
ARGF.binmode? #=> false ARGF.binmode ARGF.binmode? #=> true
Terminates option parsing. Optional parameter arg
is a string pushed back to be the first non-option argument.
Returns the binding associated with prc.
def fred(param) proc {} end b = fred(99) eval("param", b.binding) #=> 99
returns main ractor
Returns the main thread.
Terminates thr
and schedules another thread to be run, returning the terminated Thread
. If this is the main thread, or the last thread, exits the process.
Return the generated binding object from event.
Note that for :c_call
and :c_return
events, the method will return nil
, since C methods themselves do not have bindings.
Returns a Binding
object, describing the variable and method bindings at the point of call. This object can be used when calling Binding#eval
to execute the evaluated command in this environment, or extracting its local variables.
class User def initialize(name, position) @name = name @position = position end def get_binding binding end end user = User.new('Joan', 'manager') template = '{name: @name, position: @position}' # evaluate template in context of the object eval(template, user.get_binding) #=> {:name=>"Joan", :position=>"manager"}
Binding#local_variable_get
can be used to access the variables whose names are reserved Ruby keywords:
# This is valid parameter declaration, but `if` parameter can't # be accessed by name, because it is a reserved word. def validate(field, validation, if: nil) condition = binding.local_variable_get('if') return unless condition # ...Some implementation ... end validate(:name, :empty?, if: false) # skips validation validate(:name, :empty?, if: true) # performs validation
Returns the element with the minimum 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 minimum element, using the elements’ own method <=>
for comparison:
(1..4).min # => 1 (-4..-1).min # => -4 %w[d c b a].min # => "a" {foo: 0, bar: 1, baz: 2}.min # => [:bar, 1] [].min # => nil
With positive integer argument n
given, and no block, returns an array containing the first n
minimum elements that exist:
(1..4).min(2) # => [1, 2] (-4..-1).min(2) # => [-4, -3] %w[d c b a].min(2) # => ["a", "b"] {foo: 0, bar: 1, baz: 2}.min(2) # => [[:bar, 1], [:baz, 2]] [].min(2) # => []
With a block given, the block determines the minimum 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 minimum element as determined by the block:
%w[xxx x xxxx xx].min {|a, b| a.size <=> b.size } # => "x" h = {foo: 0, bar: 1, baz: 2} h.min {|pair1, pair2| pair1[1] <=> pair2[1] } # => [:foo, 0] [].min {|a, b| a <=> b } # => nil
With a block given and positive integer argument n
given, returns an array containing the first n
minimum elements that exist, as determined by the block.
%w[xxx x xxxx xx].min(2) {|a, b| a.size <=> b.size } # => ["x", "xx"] h = {foo: 0, bar: 1, baz: 2} h.min(2) {|pair1, pair2| pair1[1] <=> pair2[1] } # => [[:foo, 0], [:bar, 1]] [].min(2) {|a, b| a <=> b } # => []
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 } # => []
Returns the maximum number of group IDs allowed in the supplemental group access list:
Process.maxgroups # => 32
Sets the maximum number of group IDs allowed in the supplemental group access list.
Returns true
if and only if the scan pointer is at the beginning of the line.
s = StringScanner.new("test\ntest\n") s.bol? # => true s.scan(/te/) s.bol? # => false s.scan(/st\n/) s.bol? # => true s.terminate s.bol? # => true
Terminate the application with exit code status
, running any exit handlers that might have been defined.
Terminates the RubyGems process with the given exit_code
Returns the index of a specified element.
When argument object
is given but no block, returns the index of the first element element
for which object == element
:
a = [:foo, 'bar', 2, 'bar'] a.index('bar') # => 1
Returns nil
if no such element found.
When both argument object
and a block are given, calls the block with each successive element; returns the index of the first element for which the block returns a truthy value:
a = [:foo, 'bar', 2, 'bar'] a.index {|element| element == 'bar' } # => 1
Returns nil
if the block never returns a truthy value.
When neither an argument nor a block is given, returns a new Enumerator:
a = [:foo, 'bar', 2] e = a.index e # => #<Enumerator: [:foo, "bar", 2]:index> e.each {|element| element == 'bar' } # => 1
Related: rindex
.