Returns 0. Just for compatibility to IO
.
Returns true
; implemented only for compatibility with other stream classes.
Returns the argument unchanged. Just for compatibility to IO
.
Pushes back (“unshifts”) a character or integer onto the stream; see Character IO.
Pushes back (“unshifts”) an 8-bit byte onto the stream; see Byte IO.
Sets the scan pointer to the previous position. Only one previous position is remembered, and it changes with each scanning operation.
s = StringScanner.new('test string') s.scan(/\w+/) # => "test" s.unscan s.scan(/../) # => "te" s.scan(/\d/) # => nil s.unscan # ScanError: unscan failed: previous match record not exist
Returns a string that represents the StringScanner
object, showing:
the current position
the size of the string
the characters surrounding the scan pointer
s = StringScanner.new
(“Fri Dec 12 1975 14:39”) s.inspect # -> ‘#<StringScanner 0/21 @ “Fri D…”>’ s.scan_until /12/ # -> “Fri Dec 12” s.inspect # -> ‘#<StringScanner 10/21 “…ec 12” @ “ 1975…”>’
Returns a new String containing the hash entries:
h = {foo: 0, bar: 1, baz: 2} h.inspect # => "{:foo=>0, :bar=>1, :baz=>2}"
Hash#to_s
is an alias for Hash#inspect
.
Returns a new Hash object whose entries are all those from self
for which the block returns false
or nil
:
h = {foo: 0, bar: 1, baz: 2} h1 = h.reject {|key, value| key.start_with?('b') } h1 # => {:foo=>0}
Returns a new Enumerator if no block given:
h = {foo: 0, bar: 1, baz: 2} e = h.reject # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:reject> h1 = e.each {|key, value| key.start_with?('b') } h1 # => {:foo=>0}
Returns self
, whose remaining entries are those for which the block returns false
or nil
:
h = {foo: 0, bar: 1, baz: 2} h.reject! {|key, value| value < 2 } # => {:baz=>2}
Returns nil
if no entries are removed.
Returns a new Enumerator if no block given:
h = {foo: 0, bar: 1, baz: 2} e = h.reject! # => #<Enumerator: {:foo=>0, :bar=>1, :baz=>2}:reject!> e.each {|key, value| key.start_with?('b') } # => {:foo=>0}
Returns a copy of self
with all nil
-valued entries removed:
h = {foo: 0, bar: nil, baz: 2, bat: nil} h1 = h.compact h1 # => {:foo=>0, :baz=>2}
Returns self
with all its nil
-valued entries removed (in place):
h = {foo: 0, bar: nil, baz: 2, bat: nil} h.compact! # => {:foo=>0, :baz=>2}
Returns nil
if no entries were removed.
Methods has_key?
, key?
, and member?
are aliases for #include?.
Returns true
if key
is a key in self
, otherwise false
.
Yields each environment variable name and its value as a 2-element Array
. Returns a Hash
whose items are determined by the block. When the block returns a truthy value, the name/value pair is added to the return Hash
; otherwise the pair is ignored:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.reject { |name, value| name.start_with?('b') } # => {"foo"=>"0"}
Returns an Enumerator
if no block given:
e = ENV.reject e.each { |name, value| name.start_with?('b') } # => {"foo"=>"0"}
Similar to ENV.delete_if
, but returns nil
if no changes were made.
Yields each environment variable name and its value as a 2-element Array
, deleting each environment variable for which the block returns a truthy value, and returning ENV
(if any deletions) or nil
(if not):
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.reject! { |name, value| name.start_with?('b') } # => ENV ENV # => {"foo"=>"0"} ENV.reject! { |name, value| name.start_with?('b') } # => nil
Returns an Enumerator
if no block given:
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') e = ENV.reject! # => #<Enumerator: {"bar"=>"1", "baz"=>"2", "foo"=>"0"}:reject!> e.each { |name, value| name.start_with?('b') } # => ENV ENV # => {"foo"=>"0"} e.each { |name, value| name.start_with?('b') } # => nil
Returns the contents of the environment as a String:
ENV.replace('foo' => '0', 'bar' => '1') ENV.inspect # => "{\"bar\"=>\"1\", \"foo\"=>\"0\"}"
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)
Raises TypeError
, because ENV
is a wrapper for the process-wide environment variables and a clone is useless. Use to_h to get a copy of ENV
data as a hash.
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.
Returns “ARGF”.
Creates or retrieves cached CSV objects. For arguments and options, see CSV.new
.
This API is not Ractor-safe.
With no block given, returns a CSV object.
The first call to instance
creates and caches a CSV object:
s0 = 's0' csv0 = CSV.instance(s0) csv0.class # => CSV
Subsequent calls to instance
with that same string
or io
retrieve that same cached object:
csv1 = CSV.instance(s0) csv1.class # => CSV csv1.equal?(csv0) # => true # Same CSV object
A subsequent call to instance
with a different string
or io
creates and caches a different CSV object.
s1 = 's1' csv2 = CSV.instance(s1) csv2.equal?(csv0) # => false # Different CSV object
All the cached objects remains available:
csv3 = CSV.instance(s0) csv3.equal?(csv0) # true # Same CSV object csv4 = CSV.instance(s1) csv4.equal?(csv2) # true # Same CSV object
When a block is given, calls the block with the created or retrieved CSV object; returns the block’s return value:
CSV.instance(s0) {|csv| :foo } # => :foo
Returns an Array containing field converters; see Field Converters:
csv = CSV.new('') csv.converters # => [] csv.convert(:integer) csv.converters # => [:integer] csv.convert(proc {|x| x.to_s }) csv.converters
Notes that you need to call +Ractor.make_shareable(CSV::Converters
)+ on the main Ractor
to use this method.
With no block, installs a field converter (a Proc).
With a block, defines and installs a custom field converter.
Returns the Array of installed field converters.
Argument converter_name
, if given, should be the name of an existing field converter.
See Field Converters.
With no block, installs a field converter:
csv = CSV.new('') csv.convert(:integer) csv.convert(:float) csv.convert(:date) csv.converters # => [:integer, :float, :date]
The block, if given, is called for each field:
Argument field
is the field value.
Argument field_info
is a CSV::FieldInfo
object containing details about the field.
The examples here assume the prior execution of:
string = "foo,0\nbar,1\nbaz,2\n" path = 't.csv' File.write(path, string)
Example giving a block:
csv = CSV.open(path) csv.convert {|field, field_info| p [field, field_info]; field.upcase } csv.read # => [["FOO", "0"], ["BAR", "1"], ["BAZ", "2"]]
Output:
["foo", #<struct CSV::FieldInfo index=0, line=1, header=nil>] ["0", #<struct CSV::FieldInfo index=1, line=1, header=nil>] ["bar", #<struct CSV::FieldInfo index=0, line=2, header=nil>] ["1", #<struct CSV::FieldInfo index=1, line=2, header=nil>] ["baz", #<struct CSV::FieldInfo index=0, line=3, header=nil>] ["2", #<struct CSV::FieldInfo index=1, line=3, header=nil>]
The block need not return a String object:
csv = CSV.open(path) csv.convert {|field, field_info| field.to_sym } csv.read # => [[:foo, :"0"], [:bar, :"1"], [:baz, :"2"]]
If converter_name
is given, the block is not called:
csv = CSV.open(path) csv.convert(:integer) {|field, field_info| fail 'Cannot happen' } csv.read # => [["foo", 0], ["bar", 1], ["baz", 2]]
Raises a parse-time exception if converter_name
is not the name of a built-in field converter:
csv = CSV.open(path) csv.convert(:nosuch) => [nil] # Raises NoMethodError (undefined method `arity' for nil:NilClass) csv.read
Returns a String showing certain properties of self
:
string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n" csv = CSV.new(string, headers: true) s = csv.inspect s # => "#<CSV io_type:StringIO encoding:UTF-8 lineno:0 col_sep:\",\" row_sep:\"\\n\" quote_char:\"\\\"\" headers:true>"
Generate results and print them. (see ERB#result
)