Create a new ConstantPathOrWriteNode
node.
Create a new GlobalVariableOperatorWriteNode
node.
Create a new InstanceVariableOrWriteNode
node.
Create a new LocalVariableOperatorWriteNode
node.
Foo += bar ^^^^^^^^^^^
def foo(**bar); end
^^^^^
def foo(**); end
^^
Foo += bar ^^^^^^^^^^^
def foo(**bar); end
^^^^^
def foo(**); end
^^
def foo(bar:); end
^^^^
Returns self
.
When a block given, iterates backwards over the elements of self
, passing, in reverse order, each element to the block; returns self
:
a = [] [0, 1, 2].reverse_each {|element| a.push(element) } a # => [2, 1, 0]
Allows the array to be modified during iteration:
a = ['a', 'b', 'c'] a.reverse_each {|element| a.clear if element.start_with?('b') } a # => []
When no block given, returns a new Enumerator
.
Related: see Methods for Iterating.
With a block given, sorts the elements of self
in place; returns self.
Calls the block with each successive element; sorts elements based on the values returned from the block:
a = ['aaaa', 'bbb', 'cc', 'd'] a.sort_by! {|element| element.size } a # => ["d", "cc", "bbb", "aaaa"]
For duplicate values returned by the block, the ordering is indeterminate, and may be unstable.
With no block given, returns a new Enumerator
.
Related: see Methods for Assigning.
With a block given, calls the block with each repeated permutation of length size
of the elements of self
; each permutation is an array; returns self
. The order of the permutations is indeterminate.
If a positive integer argument size
is given, calls the block with each size
-tuple repeated permutation of the elements of self
. The number of permutations is self.size**size
.
Examples:
size
is 1:
p = [] [0, 1, 2].repeated_permutation(1) {|permutation| p.push(permutation) } p # => [[0], [1], [2]]
size
is 2:
p = [] [0, 1, 2].repeated_permutation(2) {|permutation| p.push(permutation) } p # => [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
If size
is zero, calls the block once with an empty array.
If size
is negative, does not call the block:
[0, 1, 2].repeated_permutation(-1) {|permutation| fail 'Cannot happen' }
With no block given, returns a new Enumerator
.
Related: see Methods for Combining.
With a block given, calls the block with each repeated combination of length size
of the elements of self
; each combination is an array; returns self
. The order of the combinations is indeterminate.
If a positive integer argument size
is given, calls the block with each size
-tuple repeated combination of the elements of self
. The number of combinations is (size+1)(size+2)/2
.
Examples:
size
is 1:
c = [] [0, 1, 2].repeated_combination(1) {|combination| c.push(combination) } c # => [[0], [1], [2]]
size
is 2:
c = [] [0, 1, 2].repeated_combination(2) {|combination| c.push(combination) } c # => [[0, 0], [0, 1], [0, 2], [1, 1], [1, 2], [2, 2]]
If size
is zero, calls the block once with an empty array.
If size
is negative, does not call the block:
[0, 1, 2].repeated_combination(-1) {|combination| fail 'Cannot happen' }
With no block given, returns a new Enumerator
.
Related: see Methods for Combining.
Returns self
(which is already an Integer).
Imports methods from modules. Unlike Module#include
, Refinement#import_methods
copies methods and adds them into the refinement, so the refinement is activated in the imported methods.
Note that due to method copying, only methods defined in Ruby code can be imported.
module StrUtils def indent(level) ' ' * level + self end end module M refine String do import_methods StrUtils end end using M "foo".indent(3) #=> " foo" module M refine String do import_methods Enumerable # Can't import method which is not defined with Ruby code: Enumerable#drop end end
Returns a JSON
string representing self
:
require 'json/add/complex' puts Complex(2).to_json puts Complex(2.0, 4).to_json
Output:
{"json_class":"Complex","r":2,"i":0} {"json_class":"Complex","r":2.0,"i":4}
Returns self
as an integer; converts using method to_i
in the derived class.
Of the Core and Standard Library classes, only Rational
and Complex
use this implementation.
Examples:
Rational(1, 2).to_int # => 0 Rational(2, 1).to_int # => 2 Complex(2, 0).to_int # => 2 Complex(2, 1).to_int # Raises RangeError (non-zero imaginary part)
Returns an array of the grapheme clusters in self
(see Unicode Grapheme Cluster Boundaries):
s = "\u0061\u0308-pqr-\u0062\u0308-xyz-\u0063\u0308" # => "ä-pqr-b̈-xyz-c̈" s.grapheme_clusters # => ["ä", "-", "p", "q", "r", "-", "b̈", "-", "x", "y", "z", "-", "c̈"]
Returns the Symbol
corresponding to str, creating the symbol if it did not previously exist. See Symbol#id2name
.
"Koala".intern #=> :Koala s = 'cat'.to_sym #=> :cat s == :cat #=> true s = '@cat'.to_sym #=> :@cat s == :@cat #=> true
This can also be used to create symbols that cannot be represented using the :xxx
notation.
'cat and dog'.to_sym #=> :"cat and dog"