Reads and returns the next 1-character string from the stream; raises EOFError
if already at end-of-stream. See Character IO.
f = File.open('t.txt') f.readchar # => "F" f.close f = File.open('t.rus') f.readchar.ord # => 1090 f.close
Reads and returns the next byte (in range 0..255) from the stream; raises EOFError
if already at end-of-stream. See Byte IO.
f = File.open('t.txt') f.readbyte # => 70 f.close f = File.open('t.rus') f.readbyte # => 209 f.close
Related: IO#getbyte
(will not raise EOFError
).
Repositions the stream to its beginning, setting both the position and the line number to zero; see Position and Line Number:
f = File.open('t.txt') f.tell # => 0 f.lineno # => 0 f.gets # => "First line\n" f.tell # => 12 f.lineno # => 1 f.rewind # => 0 f.tell # => 0 f.lineno # => 0 f.close
Note that this method cannot be used with streams such as pipes, ttys, and sockets.
Returns true
if the underlying file descriptor of ios will be closed automatically at its finalization, otherwise false
.
Sets auto-close flag.
f = open("/dev/null") IO.for_fd(f.fileno) # ... f.gets # may cause Errno::EBADF f = open("/dev/null") IO.for_fd(f.fileno).autoclose = false # ... f.gets # won't cause Errno::EBADF
Returns the integer file descriptor for the stream:
$stdin.fileno # => 0 $stdout.fileno # => 1 $stderr.fileno # => 2 File.open('t.txt').fileno # => 10 f.close
Iterates over the elements of self
.
With a block given and no argument, calls the block each element of the range; returns self
:
a = [] (1..5).step {|element| a.push(element) } # => 1..5 a # => [1, 2, 3, 4, 5] a = [] ('a'..'e').step {|element| a.push(element) } # => "a".."e" a # => ["a", "b", "c", "d", "e"]
With a block given and a positive integer argument n
given, calls the block with element 0
, element n
, element 2n
, and so on:
a = [] (1..5).step(2) {|element| a.push(element) } # => 1..5 a # => [1, 3, 5] a = [] ('a'..'e').step(2) {|element| a.push(element) } # => "a".."e" a # => ["a", "c", "e"]
With no block given, returns an enumerator, which will be of class Enumerator::ArithmeticSequence
if self
is numeric; otherwise of class Enumerator:
e = (1..5).step(2) # => ((1..5).step(2)) e.class # => Enumerator::ArithmeticSequence ('a'..'e').step # => #<Enumerator: ...>
Related: Range#%
.
With no argument, returns the first element of self
, if it exists:
(1..4).first # => 1 ('a'..'d').first # => "a"
With non-negative integer argument n
given, returns the first n
elements in an array:
(1..10).first(3) # => [1, 2, 3] (1..10).first(0) # => [] (1..4).first(50) # => [1, 2, 3, 4]
Raises an exception if there is no first element:
(..4).first # Raises RangeError
With no argument, returns the last element of self
, if it exists:
(1..4).last # => 4 ('a'..'d').last # => "d"
Note that last
with no argument returns the end element of self
even if exclude_end?
is true
:
(1...4).last # => 4 ('a'...'d').last # => "d"
With non-negative integer argument n
given, returns the last n
elements in an array:
(1..10).last(3) # => [8, 9, 10] (1..10).last(0) # => [] (1..4).last(50) # => [1, 2, 3, 4]
Note that last
with argument does not return the end element of self
if exclude_end?
it true
:
(1...4).last(3) # => [1, 2, 3] ('a'...'d').last(3) # => ["a", "b", "c"]
Raises an exception if there is no last element:
(1..).last # Raises RangeError
Returns an array containing the elements in self
, if a finite collection; raises an exception otherwise.
(1..4).to_a # => [1, 2, 3, 4] (1...4).to_a # => [1, 2, 3] ('a'..'d').to_a # => ["a", "b", "c", "d"]
Range#entries
is an alias for Range#to_a
.
Returns a string representation of self
, including begin.to_s
and end.to_s
:
(1..4).to_s # => "1..4" (1...4).to_s # => "1...4" (1..).to_s # => "1.." (..4).to_s # => "..4"
Note that returns from to_s
and inspect
may differ:
('a'..'d').to_s # => "a..d" ('a'..'d').inspect # => "\"a\"..\"d\""
Related: Range#inspect
.
Returns a string showing the options and string of self
:
r0 = /ab+c/ix s0 = r0.to_s # => "(?ix-m:ab+c)"
The returned string may be used as an argument to Regexp.new
, or as interpolated text for a Regexp literal:
r1 = Regexp.new(s0) # => /(?ix-m:ab+c)/ r2 = /#{s0}/ # => /(?ix-m:ab+c)/
Note that r1
and r2
are not equal to r0
because their original strings are different:
r0 == r1 # => false r0.source # => "ab+c" r1.source # => "(?ix-m:ab+c)"
Related: Regexp#inspect
.
Replaces the contents of the set with the contents of the given enumerable object and returns self.
set = Set[1, 'c', :s] #=> #<Set: {1, "c", :s}> set.replace([1, 2]) #=> #<Set: {1, 2}> set #=> #<Set: {1, 2}>
Converts the set to an array. The order of elements is uncertain.
Set[1, 2].to_a #=> [1, 2] Set[1, 'c', :s].to_a #=> [1, "c", :s]
Equivalent to Set#delete_if
, but returns nil if no changes were made. Returns an enumerator if no block is given.
Resets the internal state after modification to existing elements and returns self.
Elements will be reindexed and deduplicated.
Returns the values in self
as an array:
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.to_a # => ["Joe Smith", "123 Maple, Anytown NC", 12345]
Struct#values
and Struct#deconstruct
are aliases for Struct#to_a
.
Related: members
.
Returns a hash containing the name and value for each member:
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) h = joe.to_h h # => {:name=>"Joe Smith", :address=>"123 Maple, Anytown NC", :zip=>12345}
If a block is given, it is called with each name/value pair; the block should return a 2-element array whose elements will become a key/value pair in the returned hash:
h = joe.to_h{|name, value| [name.upcase, value.to_s.upcase]} h # => {:NAME=>"JOE SMITH", :ADDRESS=>"123 MAPLE, ANYTOWN NC", :ZIP=>"12345"}
Raises ArgumentError
if the block returns an inappropriate value.
Returns the values in self
as an array:
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.to_a # => ["Joe Smith", "123 Maple, Anytown NC", 12345]
Struct#values
and Struct#deconstruct
are aliases for Struct#to_a
.
Related: members
.
Returns a string representation of self
:
Customer = Struct.new(:name, :address, :zip) # => Customer joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.inspect # => "#<struct Customer name=\"Joe Smith\", address=\"123 Maple, Anytown NC\", zip=12345>"
Struct#to_s
is an alias for Struct#inspect
.