Returns a DateTime
whose value is the same as self
:
Date.new(2001, 2, 3).to_datetime # => #<DateTime: 2001-02-03T00:00:00+00:00>
Deserializes JSON
string by converting Julian year y
, month m
, day d
and Day of Calendar Reform sg
to Date
.
Returns a Date
object which denotes self.
Returns self.
Deserializes JSON
string by converting year y
, month m
, day d
, hour H
, minute M
, second S
, offset of
and Day of Calendar Reform sg
to DateTime
.
Returns a Date
object which denotes self.
Returns a DateTime
object which denotes self.
Returns the offset in seconds between the timezones of UTC and self
:
Time.utc(2000, 1, 1).utc_offset # => 0 Time.local(2000, 1, 1).utc_offset # => -21600 # -6*3600, or minus six hours.
Time#gmt_offset
and Time#gmtoff
are aliases for Time#utc_offset
.
Calls the block with each remaining line read from the stream; returns self
. Does nothing if already at end-of-stream; See Line IO.
With no arguments given, reads lines as determined by line separator $/
:
f = File.new('t.txt') f.each_line {|line| p line } f.each_line {|line| fail 'Cannot happen' } f.close
Output:
"First line\n" "Second line\n" "\n" "Fourth line\n" "Fifth line\n"
With only string argument sep
given, reads lines as determined by line separator sep
; see Line Separator:
f = File.new('t.txt') f.each_line('li') {|line| p line } f.close
Output:
"First li" "ne\nSecond li" "ne\n\nFourth li" "ne\nFifth li" "ne\n"
The two special values for sep
are honored:
f = File.new('t.txt') # Get all into one string. f.each_line(nil) {|line| p line } f.close
Output:
"First line\nSecond line\n\nFourth line\nFifth line\n" f.rewind # Get paragraphs (up to two line separators). f.each_line('') {|line| p line }
Output:
"First line\nSecond line\n\n" "Fourth line\nFifth line\n"
With only integer argument limit
given, limits the number of bytes in each line; see Line Limit:
f = File.new('t.txt') f.each_line(8) {|line| p line } f.close
Output:
"First li" "ne\n" "Second l" "ine\n" "\n" "Fourth l" "ine\n" "Fifth li" "ne\n"
With arguments sep
and limit
given, combines the two behaviors:
Calls with the next line as determined by line separator sep
.
But returns no more bytes than are allowed by the limit.
Optional keyword argument chomp
specifies whether line separators are to be omitted:
f = File.new('t.txt') f.each_line(chomp: true) {|line| p line } f.close
Output:
"First line" "Second line" "" "Fourth line" "Fifth line"
Returns an Enumerator
if no block is given.
IO#each
is an alias for IO#each_line
.
Calls the given block with each byte (0..255) in the stream; returns self
. See Byte IO.
f = File.new('t.rus') a = [] f.each_byte {|b| a << b } a # => [209, 130, 208, 181, 209, 129, 209, 130] f.close
Returns an Enumerator
if no block is given.
Related: IO#each_char
, IO#each_codepoint
.
Calls the given block with each codepoint in the stream; returns self
:
f = File.new('t.rus') a = [] f.each_codepoint {|c| a << c } a # => [1090, 1077, 1089, 1090] f.close
Returns an Enumerator
if no block is given.
Related: IO#each_byte
, IO#each_char
.
Returns the path associated with the IO
, or nil
if there is no path associated with the IO
. It is not guaranteed that the path exists on the filesystem.
$stdin.path # => "<STDIN>" File.open("testfile") {|f| f.path} # => "testfile"
Yields all attributes (as symbols) along with the corresponding values or returns an enumerator if no block is given.
require "ostruct" data = OpenStruct.new("country" => "Australia", :capital => "Canberra") data.each_pair.to_a # => [[:country, "Australia"], [:capital, "Canberra"]]
Deserializes JSON
string by constructing new Regexp
object with source s
(Regexp
or String
) and options o
serialized by to_json
Calls the given block with each member name/value pair; returns self
:
Customer = Struct.new(:name, :address, :zip) # => Customer joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.each_pair {|(name, value)| p "#{name} => #{value}" }
Output:
"name => Joe Smith" "address => 123 Maple, Anytown NC" "zip => 12345"
Returns an Enumerator
if no block is given.
Related: each
.
Returns an array of values from self
.
With integer arguments integers
given, returns an array containing each value given by one of integers
:
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.values_at(0, 2) # => ["Joe Smith", 12345] joe.values_at(2, 0) # => [12345, "Joe Smith"] joe.values_at(2, 1, 0) # => [12345, "123 Maple, Anytown NC", "Joe Smith"] joe.values_at(0, -3) # => ["Joe Smith", "Joe Smith"]
Raises IndexError
if any of integers
is out of range; see Array Indexes at Array
.
With integer range argument integer_range
given, returns an array containing each value given by the elements of the range; fills with nil
values for range elements larger than the structure:
joe.values_at(0..2) # => ["Joe Smith", "123 Maple, Anytown NC", 12345] joe.values_at(-3..-1) # => ["Joe Smith", "123 Maple, Anytown NC", 12345] joe.values_at(1..4) # => ["123 Maple, Anytown NC", 12345, nil, nil]
Raises RangeError
if any element of the range is negative and out of range; see Array Indexes at Array
.
Returns true if this class can be used to create an instance from a serialised JSON
string. The class has to implement a class method json_create that expects a hash as first parameter. The hash should include the required data.
Iterates over each component of the path.
Pathname.new("/usr/bin/ruby").each_filename {|filename| ... } # yields "usr", "bin", and "ruby".
Returns an Enumerator
if no block was given.
enum = Pathname.new("/usr/bin/ruby").each_filename # ... do stuff ... enum.each { |e| ... } # yields "usr", "bin", and "ruby".