Sets the value of an attribute.
require "ostruct" person = OpenStruct.new("name" => "John Smith", "age" => 70) person[:age] = 42 # equivalent to person.age = 42 person.age # => 42
Assigns a value to a member.
With symbol or string argument name
given, assigns the given value
to the named member; returns value
:
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe[:zip] = 54321 # => 54321 joe # => #<struct Customer name="Joe Smith", address="123 Maple, Anytown NC", zip=54321>
Raises NameError
if name
is not the name of a member.
With integer argument n
given, assigns the given value
to the n
-th member if n
is in range; see Array Indexes at Array
:
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe[2] = 54321 # => 54321 joe[-3] = 'Joseph Smith' # => "Joseph Smith" joe # => #<struct Customer name="Joseph Smith", address="123 Maple, Anytown NC", zip=54321>
Raises IndexError
if n
is out of range.
Sets the value to WIN32OLE
object specified by a1, a2, …
dict = WIN32OLE.new('Scripting.Dictionary') dict.add('ruby', 'RUBY') dict['ruby'] = 'Ruby' puts dict['ruby'] # => 'Ruby'
Remark: You can not use this method to set the property value.
excel = WIN32OLE.new('Excel.Application') # excel['Visible'] = true # This is error !!! excel.Visible = true # You should to use this style to set the property.
Hash#store
is an alias for Hash#[]=
.
Associates the given value
with the given key
; returns value
.
If the given key
exists, replaces its value with the given value
; the ordering is not affected (see Entry Order):
h = {foo: 0, bar: 1} h[:foo] = 2 # => 2 h.store(:bar, 3) # => 3 h # => {:foo=>2, :bar=>3}
If key
does not exist, adds the key
and value
; the new entry is last in the order (see Entry Order):
h = {foo: 0, bar: 1} h[:baz] = 2 # => 2 h.store(:bat, 3) # => 3 h # => {:foo=>0, :bar=>1, :baz=>2, :bat=>3}
ENV.store
is an alias for ENV.[]=
.
Creates, updates, or deletes the named environment variable, returning the value. Both name
and value
may be instances of String
. See Valid Names and Values.
If the named environment variable does not exist:
If value
is nil
, does nothing.
ENV.clear ENV['foo'] = nil # => nil ENV.include?('foo') # => false ENV.store('bar', nil) # => nil ENV.include?('bar') # => false
If value
is not nil
, creates the environment variable with name
and value
:
# Create 'foo' using ENV.[]=. ENV['foo'] = '0' # => '0' ENV['foo'] # => '0' # Create 'bar' using ENV.store. ENV.store('bar', '1') # => '1' ENV['bar'] # => '1'
If the named environment variable exists:
If value
is not nil
, updates the environment variable with value value
:
# Update 'foo' using ENV.[]=. ENV['foo'] = '2' # => '2' ENV['foo'] # => '2' # Update 'bar' using ENV.store. ENV.store('bar', '3') # => '3' ENV['bar'] # => '3'
If value
is nil
, deletes the environment variable:
# Delete 'foo' using ENV.[]=. ENV['foo'] = nil # => nil ENV.include?('foo') # => false # Delete 'bar' using ENV.store. ENV.store('bar', nil) # => nil ENV.include?('bar') # => false
Raises an exception if name
or value
is invalid. See Invalid Names and Values.
Creates or replaces the value for the given key
:
example_store do |store| temp.transaction do temp[:bat] = 3 end end
See also Hierarchical Values.
Raises an exception if called outside a transaction block.
set a value in ractor-local storage
Attribute Assignment—Sets or creates the value of a fiber-local variable, using either a symbol or a string.
See also Thread#[]
.
For thread-local variables, please see thread_variable_set
and thread_variable_get
.
Sets the warning flags for category
. See Warning.[]
for the categories.
Assigns the underlying string as other_string
, and sets position to zero; returns other_string
:
StringIO.open('foo') do |strio| p strio.string strio.string = 'bar' p strio.string end
Output:
"foo" "bar"
Related: StringIO#string
(returns the underlying string).
Changes the string being scanned to str
and resets the scanner. Returns str
.
Set
struct member name
, to value val
. If more arguments are specified, writes the string of bytes to the memory at the given offset
and length
.
Examples:
my_struct = struct(['int id']).malloc my_struct['id'] = 1 my_struct[0, 4] = "\x01\x00\x00\x00".b my_struct.id # => 1
Set
the value at index
to int
.
Or, set the memory at start
until length
with the contents of string
, the memory from dl_cptr
, or the memory pointed at by the memory address addr
.
Write value to a registry value named name.
If wtype is specified, the value type is it. Otherwise, the value type is depend on class of value: :Integer
REG_DWORD
:String
REG_SZ
:Array
REG_MULTI_SZ
Set
the element of WIN32OLE_VARIANT object(OLE array) to val. This method is available only when the variant type of WIN32OLE_VARIANT object is VT_ARRAY.
REMARK:
The all indices should be 0 or natural number and lower than or equal to max indices. (This point is different with Ruby Array indices.) obj = WIN32OLE_VARIANT.new([[1,2,3],[4,5,6]]) obj[0,0] = 7 obj[1,0] = 8 p obj.value # => [[7,2,3], [8,5,6]] obj[2,0] = 9 # => WIN32OLERuntimeError obj[0, -1] = 9 # => WIN32OLERuntimeError
Creates a weak reference from the given key to the given value
Set
the session data for key key
.
Assigns the field value for the given index
or header
; returns value
.
Assign field value by Integer index:
source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n" table = CSV.parse(source, headers: true) row = table[0] row[0] = 'Bat' row[1] = 3 row # => #<CSV::Row "Name":"Bat" "Value":3>
Counts backward from the last column if index
is negative:
row[-1] = 4 row[-2] = 'Bam' row # => #<CSV::Row "Name":"Bam" "Value":4>
Extends the row with nil:nil
if positive index
is not in the row:
row[4] = 5 row # => #<CSV::Row "Name":"bad" "Value":4 nil:nil nil:nil nil:5>
Raises IndexError
if negative index
is too small (too far from zero).
Assign field value by header (first found):
source = "Name,Name,Name\nFoo,Bar,Baz\n" table = CSV.parse(source, headers: true) row = table[0] row['Name'] = 'Bat' row # => #<CSV::Row "Name":"Bat" "Name":"Bar" "Name":"Baz">
Assign field value by header, ignoring offset
leading fields:
source = "Name,Name,Name\nFoo,Bar,Baz\n" table = CSV.parse(source, headers: true) row = table[0] row['Name', 2] = 4 row # => #<CSV::Row "Name":"Foo" "Name":"Bar" "Name":4>
Append new field by (new) header:
source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n" table = CSV.parse(source, headers: true) row = table[0] row['New'] = 6 row# => #<CSV::Row "Name":"foo" "Value":"0" "New":6>
Puts data onto the table.
Set
a Row by Its Integer Index
Form: table[n] = row
, n
an Integer, row
a CSV::Row instance or an Array of fields.
Access mode: :row
or :col_or_row
.
Return value: row
.
If the row exists, it is replaced:
source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n" table = CSV.parse(source, headers: true) new_row = CSV::Row.new(['Name', 'Value'], ['bat', 3]) table.by_row! # => #<CSV::Table mode:row row_count:4> return_value = table[0] = new_row return_value.equal?(new_row) # => true # Returned the row table[0].to_h # => {"Name"=>"bat", "Value"=>3}
With access mode :col_or_row
:
table.by_col_or_row! # => #<CSV::Table mode:col_or_row row_count:4> table[0] = CSV::Row.new(['Name', 'Value'], ['bam', 4]) table[0].to_h # => {"Name"=>"bam", "Value"=>4}
With an Array instead of a CSV::Row, inherits headers from the table:
array = ['bad', 5] return_value = table[0] = array return_value.equal?(array) # => true # Returned the array table[0].to_h # => {"Name"=>"bad", "Value"=>5}
If the row does not exist, extends the table by adding rows: assigns rows with nil
as needed:
table.size # => 3 table[5] = ['bag', 6] table.size # => 6 table[3] # => nil table[4]# => nil table[5].to_h # => {"Name"=>"bag", "Value"=>6}
Note that the nil
rows are actually nil
, not a row of nil
fields.
Set
a Column by Its Integer Index
Form: table[n] = array_of_fields
, n
an Integer, array_of_fields
an Array of String fields.
Access mode: :col
.
Return value: array_of_fields
.
If the column exists, it is replaced:
source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n" table = CSV.parse(source, headers: true) new_col = [3, 4, 5] table.by_col! # => #<CSV::Table mode:col row_count:4> return_value = table[1] = new_col return_value.equal?(new_col) # => true # Returned the column table[1] # => [3, 4, 5] # The rows, as revised: table.by_row! # => #<CSV::Table mode:row row_count:4> table[0].to_h # => {"Name"=>"foo", "Value"=>3} table[1].to_h # => {"Name"=>"bar", "Value"=>4} table[2].to_h # => {"Name"=>"baz", "Value"=>5} table.by_col! # => #<CSV::Table mode:col row_count:4>
If there are too few values, fills with nil
values:
table[1] = [0] table[1] # => [0, nil, nil]
If there are too many values, ignores the extra values:
table[1] = [0, 1, 2, 3, 4] table[1] # => [0, 1, 2]
If a single value is given, replaces all fields in the column with that value:
table[1] = 'bat' table[1] # => ["bat", "bat", "bat"]
Set
a Column by Its String Header
Form: table[header] = field_or_array_of_fields
, header
a String header, field_or_array_of_fields
a field value or an Array of String fields.
Access mode: :col
or :col_or_row
.
Return value: field_or_array_of_fields
.
If the column exists, it is replaced:
source = "Name,Value\nfoo,0\nbar,1\nbaz,2\n" table = CSV.parse(source, headers: true) new_col = [3, 4, 5] table.by_col! # => #<CSV::Table mode:col row_count:4> return_value = table['Value'] = new_col return_value.equal?(new_col) # => true # Returned the column table['Value'] # => [3, 4, 5] # The rows, as revised: table.by_row! # => #<CSV::Table mode:row row_count:4> table[0].to_h # => {"Name"=>"foo", "Value"=>3} table[1].to_h # => {"Name"=>"bar", "Value"=>4} table[2].to_h # => {"Name"=>"baz", "Value"=>5} table.by_col! # => #<CSV::Table mode:col row_count:4>
If there are too few values, fills with nil
values:
table['Value'] = [0] table['Value'] # => [0, nil, nil]
If there are too many values, ignores the extra values:
table['Value'] = [0, 1, 2, 3, 4] table['Value'] # => [0, 1, 2]
If the column does not exist, extends the table by adding columns:
table['Note'] = ['x', 'y', 'z'] table['Note'] # => ["x", "y", "z"] # The rows, as revised: table.by_row! table[0].to_h # => {"Name"=>"foo", "Value"=>0, "Note"=>"x"} table[1].to_h # => {"Name"=>"bar", "Value"=>1, "Note"=>"y"} table[2].to_h # => {"Name"=>"baz", "Value"=>2, "Note"=>"z"} table.by_col!
If a single value is given, replaces all fields in the column with that value:
table['Value'] = 'bat' table['Value'] # => ["bat", "bat", "bat"]
Stores value v
at key
in the GW