ENV.update
is an alias for ENV.merge!
.
Adds to ENV
each key/value pair in the given hash
; returns ENV:
ENV.replace('foo' => '0', 'bar' => '1') ENV.merge!('baz' => '2', 'bat' => '3') # => {"bar"=>"1", "bat"=>"3", "baz"=>"2", "foo"=>"0"}
Deletes the ENV
entry for a hash value that is nil
:
ENV.merge!('baz' => nil, 'bat' => nil) # => {"bar"=>"1", "foo"=>"0"}
For an already-existing name, if no block given, overwrites the ENV
value:
ENV.merge!('foo' => '4') # => {"bar"=>"1", "foo"=>"4"}
For an already-existing name, if block given, yields the name, its ENV
value, and its hash value; the block’s return value becomes the new name:
ENV.merge!('foo' => '5') { |name, env_val, hash_val | env_val + hash_val } # => {"bar"=>"1", "foo"=>"45"}
Raises an exception if a name or value is invalid (see Invalid Names and Values);
ENV.replace('foo' => '0', 'bar' => '1') ENV.merge!('foo' => '6', :bar => '7', 'baz' => '9') # Raises TypeError (no implicit conversion of Symbol into String) ENV # => {"bar"=>"1", "foo"=>"6"} ENV.merge!('foo' => '7', 'bar' => 8, 'baz' => '9') # Raises TypeError (no implicit conversion of Integer into String) ENV # => {"bar"=>"1", "foo"=>"7"}
Raises an exception if the block returns an invalid name: (see Invalid Names and Values):
ENV.merge!('bat' => '8', 'foo' => '9') { |name, env_val, hash_val | 10 } # Raises TypeError (no implicit conversion of Integer into String) ENV # => {"bar"=>"1", "bat"=>"8", "foo"=>"7"}
Note that for the exceptions above, hash pairs preceding an invalid name or value are processed normally; those following are ignored.
ENV.update
is an alias for ENV.merge!
.
Adds to ENV
each key/value pair in the given hash
; returns ENV:
ENV.replace('foo' => '0', 'bar' => '1') ENV.merge!('baz' => '2', 'bat' => '3') # => {"bar"=>"1", "bat"=>"3", "baz"=>"2", "foo"=>"0"}
Deletes the ENV
entry for a hash value that is nil
:
ENV.merge!('baz' => nil, 'bat' => nil) # => {"bar"=>"1", "foo"=>"0"}
For an already-existing name, if no block given, overwrites the ENV
value:
ENV.merge!('foo' => '4') # => {"bar"=>"1", "foo"=>"4"}
For an already-existing name, if block given, yields the name, its ENV
value, and its hash value; the block’s return value becomes the new name:
ENV.merge!('foo' => '5') { |name, env_val, hash_val | env_val + hash_val } # => {"bar"=>"1", "foo"=>"45"}
Raises an exception if a name or value is invalid (see Invalid Names and Values);
ENV.replace('foo' => '0', 'bar' => '1') ENV.merge!('foo' => '6', :bar => '7', 'baz' => '9') # Raises TypeError (no implicit conversion of Symbol into String) ENV # => {"bar"=>"1", "foo"=>"6"} ENV.merge!('foo' => '7', 'bar' => 8, 'baz' => '9') # Raises TypeError (no implicit conversion of Integer into String) ENV # => {"bar"=>"1", "foo"=>"7"}
Raises an exception if the block returns an invalid name: (see Invalid Names and Values):
ENV.merge!('bat' => '8', 'foo' => '9') { |name, env_val, hash_val | 10 } # Raises TypeError (no implicit conversion of Integer into String) ENV # => {"bar"=>"1", "bat"=>"8", "foo"=>"7"}
Note that for the exceptions above, hash pairs preceding an invalid name or value are processed normally; those following are ignored.
Returns the count of environment variables:
ENV.replace('foo' => '0', 'bar' => '1') ENV.length # => 2 ENV.size # => 2
ENV.has_key?
, ENV.member?
, and ENV.key?
are aliases for ENV.include?
.
Returns true
if there is an environment variable with the given name
:
ENV.replace('foo' => '0', 'bar' => '1') ENV.include?('foo') # => true
Returns false
if name
is a valid String
and there is no such environment variable:
ENV.include?('baz') # => false
Returns false
if name
is the empty String
or is a String
containing character '='
:
ENV.include?('') # => false ENV.include?('=') # => false
Raises an exception if name
is a String
containing the NUL character "\0"
:
ENV.include?("\0") # Raises ArgumentError (bad environment variable name: contains null byte)
Raises an exception if name
has an encoding that is not ASCII-compatible:
ENV.include?("\xa1\xa1".force_encoding(Encoding::UTF_16LE)) # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: UTF-16LE)
Raises an exception if name
is not a String:
ENV.include?(Object.new) # TypeError (no implicit conversion of Object into String)
Reads at most maxlen bytes from the ARGF
stream.
If the optional outbuf argument is present, it must reference a String
, which will receive the data. The outbuf will contain only the received data after the method call even if it is not empty at the beginning.
It raises EOFError
on end of ARGF
stream. Since ARGF
stream is a concatenation of multiple files, internally EOF is occur for each file. ARGF.readpartial
returns empty strings for EOFs except the last one and raises EOFError
for the last one.
Argument csv_string
, if given, must be a String object; defaults to a new empty String.
Arguments options
, if given, should be generating options. See Options for Generating.
Creates a new CSV object via CSV.new(csv_string, **options)
; calls the block with the CSV object, which the block may modify; returns the String generated from the CSV object.
Note that a passed String is modified by this method. Pass csv_string
.dup if the String must be preserved.
This method has one additional option: :encoding
, which sets the base Encoding
for the output if no no str
is specified. CSV
needs this hint if you plan to output non-ASCII compatible data.
Add lines:
input_string = "foo,0\nbar,1\nbaz,2\n" output_string = CSV.generate(input_string) do |csv| csv << ['bat', 3] csv << ['bam', 4] end output_string # => "foo,0\nbar,1\nbaz,2\nbat,3\nbam,4\n" input_string # => "foo,0\nbar,1\nbaz,2\nbat,3\nbam,4\n" output_string.equal?(input_string) # => true # Same string, modified
Add lines into new string, preserving old string:
input_string = "foo,0\nbar,1\nbaz,2\n" output_string = CSV.generate(input_string.dup) do |csv| csv << ['bat', 3] csv << ['bam', 4] end output_string # => "foo,0\nbar,1\nbaz,2\nbat,3\nbam,4\n" input_string # => "foo,0\nbar,1\nbaz,2\n" output_string.equal?(input_string) # => false # Different strings
Create lines from nothing:
output_string = CSV.generate do |csv| csv << ['foo', 0] csv << ['bar', 1] csv << ['baz', 2] end output_string # => "foo,0\nbar,1\nbaz,2\n"
Raises an exception if csv_string
is not a String object:
# Raises TypeError (no implicit conversion of Integer into String) CSV.generate(0)
Parses string
or io
using the specified options
.
Argument string
should be a String object; it will be put into a new StringIO
object positioned at the beginning.
Argument io
should be an IO
object that is:
Open for reading; on return, the IO
object will be closed.
Positioned at the beginning. To position at the end, for appending, use method CSV.generate
. For any other positioning, pass a preset StringIO object instead.
Argument options
: see Options for Parsing
headers
Without {option headers
} case.
These examples assume prior execution of:
string = "foo,0\nbar,1\nbaz,2\n" path = 't.csv' File.write(path, string)
With no block given, returns an Array of Arrays formed from the source.
Parse a String:
a_of_a = CSV.parse(string) a_of_a # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
Parse an open File:
a_of_a = File.open(path) do |file| CSV.parse(file) end a_of_a # => [["foo", "0"], ["bar", "1"], ["baz", "2"]]
With a block given, calls the block with each parsed row:
Parse a String:
CSV.parse(string) {|row| p row }
Output:
["foo", "0"] ["bar", "1"] ["baz", "2"]
Parse an open File:
File.open(path) do |file| CSV.parse(file) {|row| p row } end
Output:
["foo", "0"] ["bar", "1"] ["baz", "2"]
headers
With {option headers
} case.
These examples assume prior execution of:
string = "Name,Count\nfoo,0\nbar,1\nbaz,2\n" path = 't.csv' File.write(path, string)
With no block given, returns a CSV::Table
object formed from the source.
Parse a String:
csv_table = CSV.parse(string, headers: ['Name', 'Count']) csv_table # => #<CSV::Table mode:col_or_row row_count:5>
Parse an open File:
csv_table = File.open(path) do |file| CSV.parse(file, headers: ['Name', 'Count']) end csv_table # => #<CSV::Table mode:col_or_row row_count:4>
With a block given, calls the block with each parsed row, which has been formed into a CSV::Row
object:
Parse a String:
CSV.parse(string, headers: ['Name', 'Count']) {|row| p row }
Output:
# <CSV::Row "Name":"foo" "Count":"0"> # <CSV::Row "Name":"bar" "Count":"1"> # <CSV::Row "Name":"baz" "Count":"2">
Parse an open File:
File.open(path) do |file| CSV.parse(file, headers: ['Name', 'Count']) {|row| p row } end
Output:
# <CSV::Row "Name":"foo" "Count":"0"> # <CSV::Row "Name":"bar" "Count":"1"> # <CSV::Row "Name":"baz" "Count":"2">
Raises an exception if the argument is not a String object or IO object:
# Raises NoMethodError (undefined method `close' for :foo:Symbol) CSV.parse(:foo)
Sets optional filename and line number that will be used in ERB
code evaluation and error reporting. See also filename=
and lineno=
erb = ERB.new('<%= some_x %>') erb.render # undefined local variable or method `some_x' # from (erb):1 erb.location = ['file.erb', 3] # All subsequent error reporting would use new location erb.render # undefined local variable or method `some_x' # from file.erb:4
Returns true if the ipaddr is a private address. IPv4 addresses in 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 as defined in RFC 1918 and IPv6 Unique Local Addresses in fc00::/7 as defined in RFC 4193 are considered private.
Returns true
iff the current severity level allows for the printing of FATAL
messages.
Sets the severity to FATAL.
Creates a matrix where the diagonal elements are composed of values
.
Matrix.diagonal(9, 5, -3) # => 9 0 0 # 0 5 0 # 0 0 -3
Returns true
if this is a diagonal matrix. Raises an error if matrix is not square.
Returns true
if this is a permutation matrix Raises an error if matrix is not square.
Returns true
if this is a symmetric matrix. Raises an error if matrix is not square.
Returns true
if this is an antisymmetric matrix. Raises an error if matrix is not square.
Returns the determinant of the matrix.
Beware that using Float
values can yield erroneous results because of their lack of precision. Consider using exact types like Rational
or BigDecimal
instead.
Matrix[[7,6], [3,9]].determinant # => 45
deprecated; use Matrix#determinant
Returns the conjugate of the matrix.
Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]] # => 1+2i i 0 # 1 2 3 Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]].conjugate # => 1-2i -i 0 # 1 2 3