Sets the size of the internal bucket cache to size.
Iterates over the elements of range, passing each in turn to the block.
The each
method can only be used if the begin object of the range supports the succ
method. A TypeError
is raised if the object does not have succ
method defined (like Float
).
If no block is given, an enumerator is returned instead.
(10..15).each {|n| print n, ' ' } # prints: 10 11 12 13 14 15 (2.5..5).each {|n| print n, ' ' } # raises: TypeError: can't iterate from Float
By using binary search, finds a value in range which meets the given condition in O(log n) where n is the size of the range.
You can use this method in two use cases: a find-minimum mode and a find-any mode. In either case, the elements of the range must be monotone (or sorted) with respect to the block.
In find-minimum mode (this is a good choice for typical use case), the block must return true or false, and there must be a value x so that:
the block returns false for any value which is less than x, and
the block returns true for any value which is greater than or equal to x.
If x is within the range, this method returns the value x. Otherwise, it returns nil.
ary = [0, 4, 7, 10, 12] (0...ary.size).bsearch {|i| ary[i] >= 4 } #=> 1 (0...ary.size).bsearch {|i| ary[i] >= 6 } #=> 2 (0...ary.size).bsearch {|i| ary[i] >= 8 } #=> 3 (0...ary.size).bsearch {|i| ary[i] >= 100 } #=> nil (0.0...Float::INFINITY).bsearch {|x| Math.log(x) >= 0 } #=> 1.0
In find-any mode (this behaves like libc’s bsearch(3)), the block must return a number, and there must be two values x and y (x <= y) so that:
the block returns a positive number for v if v < x,
the block returns zero for v if x <= v < y, and
the block returns a negative number for v if y <= v.
This method returns any value which is within the intersection of the given range and x…y (if any). If there is no value that satisfies the condition, it returns nil.
ary = [0, 100, 100, 100, 200] (0..4).bsearch {|i| 100 - ary[i] } #=> 1, 2 or 3 (0..4).bsearch {|i| 300 - ary[i] } #=> nil (0..4).bsearch {|i| 50 - ary[i] } #=> nil
You must not mix the two modes at a time; the block must always return either true/false, or always return a number. It is undefined which value is actually picked up at each iteration.
Returns the maximum value in the range. Returns nil
if the begin value of the range larger than the end value.
Can be given an optional block to override the default comparison method a <=> b
.
(10..20).max #=> 20
Allocates space for a new object of class’s class and does not call initialize on the new instance. The returned object must be an instance of class.
klass = Class.new do def initialize(*args) @initialized = true end def initialized? @initialized || false end end klass.allocate.initialized? #=> false
Returns clean pathname of self
with consecutive slashes and useless dots removed. The filesystem is not accessed.
If consider_symlink
is true
, then a more conservative algorithm is used to avoid breaking symbolic linkages. This may retain more ..
entries than absolutely necessary, but without accessing the filesystem, this can’t be avoided.
See Pathname#realpath
.
The opposite of Pathname#absolute?
It returns false
if the pathname begins with a slash.
p = Pathname.new('/im/sure') p.relative? #=> false p = Pathname.new('not/so/sure') p.relative? #=> true
Returns the children of the directory (files and subdirectories, not recursive) as an array of Pathname
objects.
By default, the returned pathnames will have enough information to access the files. If you set with_directory
to false
, then the returned pathnames will contain the filename only.
For example:
pn = Pathname("/usr/lib/ruby/1.8") pn.children # -> [ Pathname:/usr/lib/ruby/1.8/English.rb, Pathname:/usr/lib/ruby/1.8/Env.rb, Pathname:/usr/lib/ruby/1.8/abbrev.rb, ... ] pn.children(false) # -> [ Pathname:English.rb, Pathname:Env.rb, Pathname:abbrev.rb, ... ]
Note that the results never contain the entries .
and ..
in the directory because they are not children.
Creates a full path, including any intermediate directories that don’t yet exist.
See FileUtils.mkpath
and FileUtils.mkdir_p
Returns the real (absolute) pathname for self
in the actual filesystem.
Does not contain symlinks or useless dots, ..
and .
.
All components of the pathname must exist when this method is called.
Returns the real (absolute) pathname of self
in the actual filesystem.
Does not contain symlinks or useless dots, ..
and .
.
The last component of the real pathname can be nonexistent.
See File.lstat
.
Iterates over each key-value pair in the database.
If no block is given, returns an Enumerator
.
Insert or update key-value pairs.
This method will work with any object which implements an each_pair
method, such as a Hash
.
creates a new socket object connected to host:port using TCP/IP.
If local_host:local_port is given, the socket is bound to it.
The optional last argument opts is options represented by a hash. opts may have following options:
specify the timeout in seconds.
If a block is given, the block is called with the socket. The value of the block is returned. The socket is closed when this method returns.
If no block is given, the socket is returned.
Socket.tcp("www.ruby-lang.org", 80) {|sock| sock.print "GET / HTTP/1.0\r\nHost: www.ruby-lang.org\r\n\r\n" sock.close_write puts sock.read }
iterates over the list of Addrinfo
objects obtained by Addrinfo.getaddrinfo
.
Addrinfo.foreach(nil, 80) {|x| p x } #=> #<Addrinfo: 127.0.0.1:80 TCP (:80)> # #<Addrinfo: 127.0.0.1:80 UDP (:80)> # #<Addrinfo: [::1]:80 TCP (:80)> # #<Addrinfo: [::1]:80 UDP (:80)>
returns an addrinfo object for TCP address.
Addrinfo.tcp("localhost", "smtp") #=> #<Addrinfo: 127.0.0.1:25 TCP (localhost:smtp)>