Removes all key-value pairs from hsh.
h = { "a" => 100, "b" => 200 } #=> {"a"=>100, "b"=>200} h.clear #=> {}
Returns a new hash created by using hsh’s values as keys, and the keys as values. If a key with the same value already exists in the hsh, then the last one defined will be used, the earlier value(s) will be discarded.
h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 } h.invert #=> {0=>"a", 100=>"m", 200=>"d", 300=>"y"}
If there is no key with the same value, Hash#invert
is involutive.
h = { a: 1, b: 3, c: 4 } h.invert.invert == h #=> true
The condition, no key with the same value, can be tested by comparing the size of inverted hash.
# no key with the same value h = { a: 1, b: 3, c: 4 } h.size == h.invert.size #=> true # two (or more) keys has the same value h = { a: 1, b: 3, c: 1 } h.size == h.invert.size #=> false
Adds the contents of the given hashes to the receiver.
If no block is given, entries with duplicate keys are overwritten with the values from each other_hash
successively, otherwise the value for each duplicate key is determined by calling the block with the key, its value in the receiver and its value in each other_hash
.
h1 = { "a" => 100, "b" => 200 } h1.merge! #=> {"a"=>100, "b"=>200} h1 #=> {"a"=>100, "b"=>200} h1 = { "a" => 100, "b" => 200 } h2 = { "b" => 246, "c" => 300 } h1.merge!(h2) #=> {"a"=>100, "b"=>246, "c"=>300} h1 #=> {"a"=>100, "b"=>246, "c"=>300} h1 = { "a" => 100, "b" => 200 } h2 = { "b" => 246, "c" => 300 } h3 = { "b" => 357, "d" => 400 } h1.merge!(h2, h3) #=> {"a"=>100, "b"=>357, "c"=>300, "d"=>400} h1 #=> {"a"=>100, "b"=>357, "c"=>300, "d"=>400} h1 = { "a" => 100, "b" => 200 } h2 = { "b" => 246, "c" => 300 } h3 = { "b" => 357, "d" => 400 } h1.merge!(h2, h3) {|key, v1, v2| v1 } #=> {"a"=>100, "b"=>200, "c"=>300, "d"=>400} h1 #=> {"a"=>100, "b"=>200, "c"=>300, "d"=>400}
Hash#update
is an alias for Hash#merge!
.
Returns a new hash that combines the contents of the receiver and the contents of the given hashes.
If no block is given, entries with duplicate keys are overwritten with the values from each other_hash
successively, otherwise the value for each duplicate key is determined by calling the block with the key, its value in the receiver and its value in each other_hash
.
When called without any argument, returns a copy of the receiver.
h1 = { "a" => 100, "b" => 200 } h2 = { "b" => 246, "c" => 300 } h3 = { "b" => 357, "d" => 400 } h1.merge #=> {"a"=>100, "b"=>200} h1.merge(h2) #=> {"a"=>100, "b"=>246, "c"=>300} h1.merge(h2, h3) #=> {"a"=>100, "b"=>357, "c"=>300, "d"=>400} h1.merge(h2) {|key, oldval, newval| newval - oldval} #=> {"a"=>100, "b"=>46, "c"=>300} h1.merge(h2, h3) {|key, oldval, newval| newval - oldval} #=> {"a"=>100, "b"=>311, "c"=>300, "d"=>400} h1 #=> {"a"=>100, "b"=>200}
Returns a new hash with the nil values/key pairs removed
h = { a: 1, b: false, c: nil } h.compact #=> { a: 1, b: false } h #=> { a: 1, b: false, c: nil }
Removes all nil values from the hash. Returns nil if no changes were made, otherwise returns the hash.
h = { a: 1, b: false, c: nil } h.compact! #=> { a: 1, b: false }
Returns true
if the given key is present in hsh.
h = { "a" => 100, "b" => 200 } h.has_key?("a") #=> true h.has_key?("z") #=> false
Note that include?
and member?
do not test member equality using ==
as do other Enumerables.
See also Enumerable#include?
Removes every environment variable.
Returns a copy of the environment for entries where the block returns true.
Returns an Enumerator
if no block was given.
ENV.filter
is an alias for ENV.select
.
Equivalent to ENV.keep_if
but returns nil
if no changes were made.
ENV.filter!
is an alias for ENV.select!
.
Returns a copy of the environment for entries where the block returns true.
Returns an Enumerator
if no block was given.
ENV.filter
is an alias for ENV.select
.
Equivalent to ENV.keep_if
but returns nil
if no changes were made.
ENV.filter!
is an alias for ENV.select!
.
Returns a new hash created by using environment variable names as values and values as names.
Returns true when there are no environment variables
Returns true
if there is an environment variable with the given name
.
Returns the ARGV
array, which contains the arguments passed to your script, one per element.
For example:
$ ruby argf.rb -v glark.txt ARGF.argv #=> ["-v", "glark.txt"]
Reads the next character from ARGF
and returns it as a String
. Raises an EOFError
after the last character of the last file has been read.
For example:
$ echo "foo" > file $ ruby argf.rb file ARGF.readchar #=> "f" ARGF.readchar #=> "o" ARGF.readchar #=> "o" ARGF.readchar #=> "\n" ARGF.readchar #=> end of file reached (EOFError)
Seeks to offset amount (an Integer
) in the ARGF
stream according to the value of whence. See IO#seek
for further details.
Returns the current filename. “-” is returned when the current file is STDIN.
For example:
$ echo "foo" > foo $ echo "bar" > bar $ echo "glark" > glark $ ruby argf.rb foo bar glark ARGF.filename #=> "foo" ARGF.read(5) #=> "foo\nb" ARGF.filename #=> "bar" ARGF.skip ARGF.filename #=> "glark"
Closes the current file and skips to the next file in ARGV. If there are no more files to open, just closes the current file. STDIN
will not be closed.
For example:
$ ruby argf.rb foo bar ARGF.filename #=> "foo" ARGF.close ARGF.filename #=> "bar" ARGF.close
Returns true if the current file has been closed; false otherwise. Use ARGF.close
to actually close the current file.
Synonym for $stdin.
This method is an alias for http_header
, when HTML5
tag maker is inactive.
NOTE: use http_header
to create HTTP header blocks, this alias is only provided for backwards compatibility.
Using header
with the HTML5
tag maker will create a <header> element.
This method is a convenience for building Unix-like filters for CSV
data. Each row is yielded to the provided block which can alter it as needed. After the block returns, the row is appended to output
altered or not.
The input
and output
arguments can be anything CSV::new()
accepts (generally String
or IO
objects). If not given, they default to ARGF
and $stdout
.
The options
parameter is also filtered down to CSV::new()
after some clever key parsing. Any key beginning with :in_
or :input_
will have that leading identifier stripped and will only be used in the options
Hash
for the input
object. Keys starting with :out_
or :output_
affect only output
. All other keys are assigned to both objects.
The :output_row_sep
option
defaults to $INPUT_RECORD_SEPARATOR
($/
).
This method wraps a String
you provide, or an empty default String
, in a CSV
object which is passed to the provided block. You can use the block to append CSV
rows to the String
and when the block exits, the final String
will be returned.
Note that a passed String
is modified by this method. Call dup() before passing if you need a new String
.
The options
parameter can be anything CSV::new()
understands. This method understands an additional :encoding
parameter when not passed a String
to set the base Encoding
for the output. CSV
needs this hint if you plan to output non-ASCII compatible data.