Returns self
.
Sorts the elements of self
in place, using an ordering determined by the block; returns self.
Calls the block with each successive element; sorts elements based on the values returned from the block.
For duplicates returned by the block, the ordering is indeterminate, and may be unstable.
This example sorts strings based on their sizes:
a = ['aaaa', 'bbb', 'cc', 'd'] a.sort_by! {|element| element.size } a # => ["d", "cc", "bbb", "aaaa"]
Returns a new Enumerator
if no block given:
a = ['aaaa', 'bbb', 'cc', 'd'] a.sort_by! # => #<Enumerator: ["aaaa", "bbb", "cc", "d"]:sort_by!>
Searches self
as described at method bsearch
, but returns the index of the found element instead of the element itself.
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.
Returns true
if self
contains only ASCII characters, false
otherwise:
'abc'.ascii_only? # => true "abc\u{6666}".ascii_only? # => false
Returns the dirpath
string that was used to create self
(or nil
if created by method Dir.for_fd
):
Dir.new('example').path # => "example"
Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless dir_string is given, in which case it will be used as the starting point. If the given pathname starts with a “~
” it is NOT expanded, it is treated as a normal directory name.
File.absolute_path("~oracle/bin") #=> "<relative_path>/~oracle/bin"
Returns true
if file_name
is an absolute path, and false
otherwise.
File.absolute_path?("c:/foo") #=> false (on Linux), true (on Windows)
Returns true
if the named file is writable by the real user and group id of this process. See access(3).
Note that some OS-level security features may cause this to return true even though the file is not writable by the real user/group.
If file_name is writable by others, returns an integer representing the file permission bits of file_name. Returns nil
otherwise. The meaning of the bits is platform dependent; on Unix systems, see stat(2)
.
file_name can be an IO
object.
File.world_writable?("/tmp") #=> 511 m = File.world_writable?("/tmp") sprintf("%o", m) #=> "777"
Returns the locale charmap name. It returns nil if no appropriate information.
Debian GNU/Linux LANG=C Encoding.locale_charmap #=> "ANSI_X3.4-1968" LANG=ja_JP.EUC-JP Encoding.locale_charmap #=> "EUC-JP" SunOS 5 LANG=C Encoding.locale_charmap #=> "646" LANG=ja Encoding.locale_charmap #=> "eucJP"
The result is highly platform dependent. So Encoding.find(Encoding.locale_charmap)
may cause an error. If you need some encoding object even for unknown locale, Encoding.find
(“locale”) can be used.
Iterates the given block for each element with an index, which starts from offset
. If no block is given, returns a new Enumerator
that includes the index, starting from offset
offset
the starting index to use
Iterates the given block for each element with an arbitrary object, obj
, and returns obj
If no block is given, returns a new Enumerator
.
to_three = Enumerator.new do |y| 3.times do |x| y << x end end to_three_with_string = to_three.with_object("foo") to_three_with_string.each do |x,string| puts "#{string}: #{x}" end # => foo: 0 # => foo: 1 # => foo: 2
Returns the singleton class of obj. This method creates a new singleton class if obj does not have one.
If obj is nil
, true
, or false
, it returns NilClass
, TrueClass
, or FalseClass
, respectively. If obj is an Integer
, a Float
or a Symbol
, it raises a TypeError
.
Object.new.singleton_class #=> #<Class:#<Object:0xb7ce1e24>> String.singleton_class #=> #<Class:String> nil.singleton_class #=> NilClass
Returns an array of the names of singleton methods for obj. If the optional all parameter is true, the list will include methods in modules included in obj. Only public and protected singleton methods are returned.
module Other def three() end end class Single def Single.four() end end a = Single.new def a.one() end class << a include Other def two() end end Single.singleton_methods #=> [:four] a.singleton_methods(false) #=> [:two, :one] a.singleton_methods #=> [:two, :one, :three]