$foo = 1 ^^^^^^^^
$foo, $bar = 1 ^^^^ ^^^^
@foo = 1 ^^^^^^^^
@foo, @bar = 1 ^^^^ ^^^^
def foo(**bar); end
^^^^^
def foo(**); end
^^
foo = 1 ^^^^^^^
foo, bar = 1 ^^^ ^^^
def foo(bar:); end
^^^^
Returns self
.
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.
Returns the integer index of the element from self
found by a binary search, or nil
if the search found no suitable element.
See Binary Searching.
Related: see Methods for Fetching.
Returns the number of bits of the value of self
, which is the bit position of the highest-order bit that is different from the sign bit (where the least significant bit has bit position 1). If there is no such bit (zero or minus one), returns zero.
This method returns ceil(log2(self < 0 ? -self : self + 1))
>.
(-2**1000-1).bit_length # => 1001 (-2**1000).bit_length # => 1000 (-2**1000+1).bit_length # => 1000 (-2**12-1).bit_length # => 13 (-2**12).bit_length # => 12 (-2**12+1).bit_length # => 12 -0x101.bit_length # => 9 -0x100.bit_length # => 8 -0xff.bit_length # => 8 -2.bit_length # => 1 -1.bit_length # => 0 0.bit_length # => 0 1.bit_length # => 1 0xff.bit_length # => 8 0x100.bit_length # => 9 (2**12-1).bit_length # => 12 (2**12).bit_length # => 13 (2**12+1).bit_length # => 13 (2**1000-1).bit_length # => 1000 (2**1000).bit_length # => 1001 (2**1000+1).bit_length # => 1001
For Integer n, this method can be used to detect overflow in Array#pack
:
if n.bit_length < 32 [n].pack('l') # No overflow. else raise 'Overflow' end
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
Methods Complex#as_json
and Complex.json_create
may be used to serialize and deserialize a Complex object; see Marshal
.
Method Complex#as_json
serializes self
, returning a 2-element hash representing self
:
require 'json/add/complex' x = Complex(2).as_json # => {"json_class"=>"Complex", "r"=>2, "i"=>0} y = Complex(2.0, 4).as_json # => {"json_class"=>"Complex", "r"=>2.0, "i"=>4}
Method JSON.create
deserializes such a hash, returning a Complex object:
Complex.json_create(x) # => (2+0i) Complex.json_create(y) # => (2.0+4i)
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 whether self
ends with any of the given strings
.
Returns true
if any given string matches the end, false
otherwise:
'hello'.end_with?('ello') #=> true 'hello'.end_with?('heaven', 'ello') #=> true 'hello'.end_with?('heaven', 'paradise') #=> false 'тест'.end_with?('т') # => true 'こんにちは'.end_with?('は') # => true
Related: String#start_with?
.
Calls the given block with each successive character from self
; returns self
:
'hello'.each_char {|char| print char, ' ' } print "\n" 'тест'.each_char {|char| print char, ' ' } print "\n" 'こんにちは'.each_char {|char| print char, ' ' } print "\n"
Output:
h e l l o т е с т こ ん に ち は
Returns an enumerator if no block is given.