Returns the String created by generating CSV from ary
using the specified options
.
Argument ary
must be an Array.
Special options:
Option :row_sep
defaults to "\n"> on Ruby 3.0 or later and <tt>$INPUT_RECORD_SEPARATOR
($/
) otherwise.:
$INPUT_RECORD_SEPARATOR # => "\n"
This method accepts an additional option, :encoding
, which sets the base Encoding
for the output. This method will try to guess your Encoding
from the first non-nil
field in row
, if possible, but you may need to use this parameter as a backup plan.
For other options
, see Options for Generating.
Returns the String generated from an Array:
CSV.generate_line(['foo', '0']) # => "foo,0\n"
Raises an exception if ary
is not an Array:
# Raises NoMethodError (undefined method `find' for :foo:Symbol) CSV.generate_line(:foo)
Returns the String created by generating CSV from using the specified options
.
Argument rows
must be an Array of row. Row
is Array of String or CSV::Row.
Special options:
Option :row_sep
defaults to "\n"
on Ruby 3.0 or later and $INPUT_RECORD_SEPARATOR
($/
) otherwise.:
$INPUT_RECORD_SEPARATOR # => "\n"
This method accepts an additional option, :encoding
, which sets the base Encoding
for the output. This method will try to guess your Encoding
from the first non-nil
field in row
, if possible, but you may need to use this parameter as a backup plan.
For other options
, see Options for Generating.
Returns the String generated from an
CSV.generate_lines([['foo', '0'], ['bar', '1'], ['baz', '2']]) # => "foo,0\nbar,1\nbaz,2\n"
Raises an exception
# Raises NoMethodError (undefined method `each' for :foo:Symbol) CSV.generate_lines(:foo)
Returns the data created by parsing the first line of 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
, returns the first row as a new Array.
These examples assume prior execution of:
string = "foo,0\nbar,1\nbaz,2\n" path = 't.csv' File.write(path, string)
Parse the first line from a String object:
CSV.parse_line(string) # => ["foo", "0"]
Parse the first line from a File
object:
File.open(path) do |file| CSV.parse_line(file) # => ["foo", "0"] end # => ["foo", "0"]
Returns nil
if the argument is an empty String:
CSV.parse_line('') # => nil
headers
With {option headers
}, returns the first row as a CSV::Row
object.
These examples assume prior execution of:
string = "Name,Count\nfoo,0\nbar,1\nbaz,2\n" path = 't.csv' File.write(path, string)
Parse the first line from a String object:
CSV.parse_line(string, headers: true) # => #<CSV::Row "Name":"foo" "Count":"0">
Parse the first line from a File
object:
File.open(path) do |file| CSV.parse_line(file, headers: true) end # => #<CSV::Row "Name":"foo" "Count":"0">
Raises an exception if the argument is nil
:
# Raises ArgumentError (Cannot parse nil as CSV): CSV.parse_line(nil)
Returns the value that determines whether illegal input is to be handled; used for parsing; see {Option liberal_parsing
}:
CSV.new('').liberal_parsing? # => false
Return the appropriate error message in POSIX-defined format. If no error has occurred, returns nil
.
Returns a string for DNS reverse lookup compatible with RFC3172.
Returns the Ruby source filename and line number of the binding object.
Adjust the log level during the block execution for the current Fiber
only
logger.with_level(:debug) do logger.debug { "Hello" } end
Returns the usable width for out
. As the width of out
:
If out
is assigned to a tty device, its width is used.
Otherwise, or it could not get the value, the COLUMN
environment variable is assumed to be set to the width.
If COLUMN
is not set to a non-zero number, 80 is assumed.
And finally, returns the above width value - 1.
This -1 is for Windows command prompt, which moves the cursor to the next line if it reaches the last column.
Returns match and captures at the given indexes
, which may include any mixture of:
Integers.
Ranges.
Names (strings and symbols).
Examples:
m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie") # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8"> m.values_at(0, 2, -2) # => ["HX1138", "X", "113"] m.values_at(1..2, -1) # => ["H", "X", "8"] m = /(?<a>\d+) *(?<op>[+\-*\/]) *(?<b>\d+)/.match("1 + 2") # => #<MatchData "1 + 2" a:"1" op:"+" b:"2"> m.values_at(0, 1..2, :a, :b, :op) # => ["1 + 2", "1", "+", "1", "2", "+"]
Returns the substring of the target string from its beginning up to the first match in self
(that is, self[0]
); equivalent to regexp global variable $`
:
m = /(.)(.)(\d+)(\d)/.match("THX1138.") # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8"> m[0] # => "HX1138" m.pre_match # => "T"
Related: MatchData#post_match
.
Returns the substring of the target string from the end of the first match in self
(that is, self[0]
) to the end of the string; equivalent to regexp global variable $'
:
m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie") # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8"> m[0] # => "HX1138" m.post_match # => ": The Movie"\
Related: MatchData.pre_match
.
This is similar to PrettyPrint::format
but the result has no breaks.
maxwidth
, newline
and genspace
are ignored.
The invocation of breakable
in the block doesn’t break a line and is treated as just an invocation of text
.
Load the given PStore
file. If read_only
is true, the unmarshalled Hash
will be returned. If read_only
is false, a 3-tuple will be returned: the unmarshalled Hash
, a checksum of the data, and the size of the data.
Returns the Ruby source filename and line number containing this proc or nil
if this proc was not defined in Ruby (i.e. native).
Returns the Ruby source filename and line number containing this method or nil if this method was not defined in Ruby (i.e. native).
Returns the Ruby source filename and line number containing this method or nil if this method was not defined in Ruby (i.e. native).
Returns an array of the names of the thread-local variables (as Symbols).
thr = Thread.new do Thread.current.thread_variable_set(:cat, 'meow') Thread.current.thread_variable_set("dog", 'woof') end thr.join #=> #<Thread:0x401b3f10 dead> thr.thread_variables #=> [:dog, :cat]
Note that these are not fiber local variables. Please see Thread#[]
and Thread#thread_variable_get
for more details.
Returns true
if the given string (or symbol) exists as a thread-local variable.
me = Thread.current me.thread_variable_set(:oliver, "a") me.thread_variable?(:oliver) #=> true me.thread_variable?(:stanley) #=> false
Note that these are not fiber local variables. Please see Thread#[]
and Thread#thread_variable_get
for more details.