Yields self
with disabling echo back.
STDIN.noecho(&:gets)
will read and return a line without echo back.
You must require ‘io/console’ to use this method.
Returns status information for ios as an object of type File::Stat
.
f = File.new("testfile") s = f.stat "%o" % s.mode #=> "100644" s.blksize #=> 4096 s.atime #=> Wed Apr 09 08:53:54 CDT 2003
Executes the block for every line in the named I/O port, where lines are separated by sep.
If no block is given, an enumerator is returned instead.
IO.foreach("testfile") {|x| print "GOT ", x }
produces:
GOT This is line one GOT This is line two GOT This is line three GOT And so on...
If the last argument is a hash, it’s the keyword argument to open. See IO.readlines
for details about getline_args. And see also IO.read
for details about open_args.
If obj is Numeric
, write the character whose code is the least-significant byte of obj. If obj is String
, write the first character of obj to ios. Otherwise, raise TypeError
.
$stdout.putc "A" $stdout.putc 65
produces:
AA
Executes the block for every line in ios, where lines are separated by sep. ios must be opened for reading or an IOError
will be raised.
If no block is given, an enumerator is returned instead.
f = File.new("testfile") f.each {|line| puts "#{f.lineno}: #{line}" }
produces:
1: This is line one 2: This is line two 3: This is line three 4: And so on...
See IO.readlines
for details about getline_args.
This is a deprecated alias for each_char
.
Immediately writes all buffered data in ios to disk.
If the underlying operating system does not support fdatasync(2), IO#fsync
is called instead (which might raise a NotImplementedError
).
Reads a one-character string from ios. Returns nil
if called at end of file.
f = File.new("testfile") f.getc #=> "h" f.getc #=> "e"
Reads a one-character string from ios. Raises an EOFError
on end of file.
f = File.new("testfile") f.readchar #=> "h" f.readchar #=> "e"
Pushes back one character (passed as a parameter) onto ios, such that a subsequent buffered character read will return it. Only one character may be pushed back before a subsequent read operation (that is, you will be able to read only the last of several characters that have been pushed back). Has no effect with unbuffered reads (such as IO#sysread
).
f = File.new("testfile") #=> #<File:testfile> c = f.getc #=> "8" f.ungetc(c) #=> nil f.getc #=> "8"
Returns true
if ios is associated with a terminal device (tty), false
otherwise.
File.new("testfile").isatty #=> false File.new("/dev/tty").isatty #=> true
Puts ios into binary mode. Once a stream is in binary mode, it cannot be reset to nonbinary mode.
newline conversion disabled
encoding conversion disabled
content is treated as ASCII-8BIT
Returns true
if ios is binmode.
Executes block for each key in the database, passing the key and the corresponding value as a parameter.
Adds the key-value pairs of other to gdbm, overwriting entries with duplicate keys with those from other. other must have an each_pair
method.
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. Returns nil
if the begin value of an exclusive range is equal to 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