Returns the substring of self
specified by the arguments.
When the single Integer argument index
is given, returns the 1-character substring found in self
at offset index
:
'bar'[2] # => "r"
Counts backward from the end of self
if index
is negative:
'foo'[-3] # => "f"
Returns nil
if index
is out of range:
'foo'[3] # => nil 'foo'[-4] # => nil
When the two Integer arguments start
and length
are given, returns the substring of the given length
found in self
at offset start
:
'foo'[0, 2] # => "fo" 'foo'[0, 0] # => ""
Counts backward from the end of self
if start
is negative:
'foo'[-2, 2] # => "oo"
Special case: returns a new empty String if start
is equal to the length of self
:
'foo'[3, 2] # => ""
Returns nil
if start
is out of range:
'foo'[4, 2] # => nil 'foo'[-4, 2] # => nil
Returns the trailing substring of self
if length
is large:
'foo'[1, 50] # => "oo"
Returns nil
if length
is negative:
'foo'[0, -1] # => nil
When the single Range argument range
is given, derives start
and length
values from the given range
, and returns values as above:
'foo'[0..1]
is equivalent to 'foo'[0, 2]
.
'foo'[0...1]
is equivalent to 'foo'[0, 1]
.
When the Regexp argument regexp
is given, and the capture
argument is 0
, returns the first matching substring found in self
, or nil
if none found:
'foo'[/o/] # => "o" 'foo'[/x/] # => nil s = 'hello there' s[/[aeiou](.)\1/] # => "ell" s[/[aeiou](.)\1/, 0] # => "ell"
If argument capture
is given and not 0
, it should be either an Integer capture group index or a String or Symbol capture group name; the method call returns only the specified capture (see Regexp Capturing):
s = 'hello there' s[/[aeiou](.)\1/, 1] # => "l" s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "non_vowel"] # => "l" s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, :vowel] # => "e"
If an invalid capture group index is given, nil
is returned. If an invalid capture group name is given, IndexError
is raised.
When the single String argument substring
is given, returns the substring from self
if found, otherwise nil
:
'foo'['oo'] # => "oo" 'foo'['xx'] # => nil
String#slice
is an alias for String#[]
.
Deletes the specified portion from str, and returns the portion deleted.
string = "this is a string" string.slice!(2) #=> "i" string.slice!(3..6) #=> " is " string.slice!(/s.*t/) #=> "sa st" string.slice!("r") #=> "r" string #=> "thing"
Searches sep or pattern (regexp) in the string and returns the part before it, the match, and the part after it. If it is not found, returns two empty strings and str.
"hello".partition("l") #=> ["he", "l", "lo"] "hello".partition("x") #=> ["hello", "", ""] "hello".partition(/.l/) #=> ["h", "el", "lo"]
Searches sep or pattern (regexp) in the string from the end of the string, and returns the part before it, the match, and the part after it. If it is not found, returns two empty strings and str.
"hello".rpartition("l") #=> ["hel", "l", "o"] "hello".rpartition("x") #=> ["", "", "hello"] "hello".rpartition(/.l/) #=> ["he", "ll", "o"]
The match from the end means starting at the possible last position, not the last of longest matches.
"hello".rpartition(/l+/) #=> ["hel", "l", "o"]
To partition at the last longest match, needs to combine with negative lookbehind.
"hello".rpartition(/(?<!l)l+/) #=> ["he", "ll", "o"]
Or String#partition
with negative lookforward.
"hello".partition(/l+(?!.*l)/) #=> ["he", "ll", "o"]
Returns a copied string whose encoding is ASCII-8BIT.
The first form returns a copy of str
transcoded to encoding encoding
. The second form returns a copy of str
transcoded from src_encoding to dst_encoding. The last form returns a copy of str
transcoded to Encoding.default_internal
.
By default, the first and second form raise Encoding::UndefinedConversionError
for characters that are undefined in the destination encoding, and Encoding::InvalidByteSequenceError
for invalid byte sequences in the source encoding. The last form by default does not raise exceptions but uses replacement strings.
The options
keyword arguments give details for conversion. The arguments are:
If the value is :replace
, encode
replaces invalid byte sequences in str
with the replacement character. The default is to raise the Encoding::InvalidByteSequenceError
exception
If the value is :replace
, encode
replaces characters which are undefined in the destination encoding with the replacement character. The default is to raise the Encoding::UndefinedConversionError
.
Sets the replacement string to the given value. The default replacement string is “uFFFD” for Unicode encoding forms, and “?” otherwise.
Sets the replacement string by the given object for undefined character. The object should be a Hash
, a Proc
, a Method
, or an object which has [] method. Its key is an undefined character encoded in the source encoding of current transcoder. Its value can be any encoding until it can be converted into the destination encoding of the transcoder.
The value must be :text
or :attr
. If the value is :text
encode
replaces undefined characters with their (upper-case hexadecimal) numeric character references. ‘&’, ‘<’, and ‘>’ are converted to “&”, “<”, and “>”, respectively. If the value is :attr
, encode
also quotes the replacement result (using ‘“’), and replaces ‘”’ with “"”.
Replaces LF (“n”) with CR (“r”) if value is true.
Replaces LF (“n”) with CRLF (“rn”) if value is true.
Replaces CRLF (“rn”) and CR (“r”) with LF (“n”) if value is true.
The first form transcodes the contents of str from str.encoding to encoding
. The second form transcodes the contents of str from src_encoding to dst_encoding. The options
keyword arguments give details for conversion. See String#encode
for details. Returns the string even if no changes were made.
Get the address as an Integer
for the function named name
. The function is searched via dlsym on RTLD_NEXT.
See man(3) dlsym() for more info.
Get the address as an Integer
for the function named name
.
Fetch struct member name
if only one argument is specified. If two arguments are specified, the first is an offset and the second is a length and this method returns the string of length
bytes beginning at offset
.
Examples:
my_struct = struct(['int id']).malloc my_struct.id = 1 my_struct['id'] # => 1 my_struct[0, 4] # => "\x01\x00\x00\x00".b
Get the underlying pointer for ruby object val
and return it as a Fiddle::Pointer
object.
Returns integer stored at index.
If start and length are given, a string containing the bytes from start of length will be returned.
Get a specific section from the current configuration
Given the following configurating file being loaded:
config = OpenSSL::Config.load('foo.cnf') #=> #<OpenSSL::Config sections=["default"]> puts config.to_s #=> [ default ] # foo=bar
You can get a hash of the specific section like so:
config['default'] #=> {"foo"=>"bar"}
Read a registry value named name and return its value data. The class of value is same as read
method returns.
If the value type is REG_EXPAND_SZ, returns value data whose environment variables are replaced. If the value type is neither REG_SZ, REG_MULTI_SZ, REG_DWORD, REG_DWORD_BIG_ENDIAN, nor REG_QWORD, TypeError
is raised.
The meaning of rtype is same as read
method.
Retrieves a weakly referenced object with the given key
Retrieve the session data for key key
.
Returns data from the table; does not modify the table.
The expression table[n]
, where n
is a non-negative Integer, returns the +n+th row of the table, if that row exists, and if the access mode is :row
or :col_or_row
:
source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n" table = CSV.parse(source, headers: true) table.by_row! # => #<CSV::Table mode:row row_count:4> table[1] # => #<CSV::Row "Name":"bar" "Value":"1"> table.by_col_or_row! # => #<CSV::Table mode:col_or_row row_count:4> table[1] # => #<CSV::Row "Name":"bar" "Value":"1">
Counts backward from the last row if n
is negative:
table[-1] # => #<CSV::Row "Name":"baz" "Value":"2">
Returns nil
if n
is too large or too small:
table[4] # => nil table[-4] => nil
Raises an exception if the access mode is :row
and n
is not an Integer-convertible object.
table.by_row! # => #<CSV::Table mode:row row_count:4> # Raises TypeError (no implicit conversion of String into Integer): table['Name']
The expression table[range]
, where range
is a Range
object, returns rows from the table, beginning at row range.first
, if those rows exist, and if the access mode is :row
or :col_or_row
:
source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n" table = CSV.parse(source, headers: true) table.by_row! # => #<CSV::Table mode:row row_count:4> rows = table[1..2] # => #<CSV::Row "Name":"bar" "Value":"1"> rows # => [#<CSV::Row "Name":"bar" "Value":"1">, #<CSV::Row "Name":"baz" "Value":"2">] table.by_col_or_row! # => #<CSV::Table mode:col_or_row row_count:4> rows = table[1..2] # => #<CSV::Row "Name":"bar" "Value":"1"> rows # => [#<CSV::Row "Name":"bar" "Value":"1">, #<CSV::Row "Name":"baz" "Value":"2">]
If there are too few rows, returns all from range.first
to the end:
rows = table[1..50] # => #<CSV::Row "Name":"bar" "Value":"1"> rows # => [#<CSV::Row "Name":"bar" "Value":"1">, #<CSV::Row "Name":"baz" "Value":"2">]
Special case: if range.start == table.size
, returns an empty Array:
table[table.size..50] # => []
If range.end
is negative, calculates the ending index from the end:
rows = table[0..-1] rows # => [#<CSV::Row "Name":"foo" "Value":"0">, #<CSV::Row "Name":"bar" "Value":"1">, #<CSV::Row "Name":"baz" "Value":"2">]
If range.start
is negative, calculates the starting index from the end:
rows = table[-1..2] rows # => [#<CSV::Row "Name":"baz" "Value":"2">]
If range.start
is larger than table.size
, returns nil
:
table[4..4] # => nil
The expression table[header]
, where header
is a String, returns column values (Array of Strings) if the column exists and if the access mode is :col
or :col_or_row
:
source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n" table = CSV.parse(source, headers: true) table.by_col! # => #<CSV::Table mode:col row_count:4> table['Name'] # => ["foo", "bar", "baz"] table.by_col_or_row! # => #<CSV::Table mode:col_or_row row_count:4> col = table['Name'] col # => ["foo", "bar", "baz"]
Modifying the returned column values does not modify the table:
col[0] = 'bat' col # => ["bat", "bar", "baz"] table['Name'] # => ["foo", "bar", "baz"]
Returns an Array of nil
values if there is no such column:
table['Nosuch'] # => [nil, nil, nil]
Retrieves key
from the GW