We use accessing and setting values to get or change information stored in variables or data structures.
When you want to read a value stored in a variable or container.
When you need to update or change a value in a list or hash.
When you want to check what is inside a data structure before using it.
When you want to save new information into a variable or data structure.
When you want to keep track of changes in your program's data.
Syntax
Ruby
# Accessing a value
value = container[key]
# Setting a value
container[key] = new_value
In Ruby, you use square brackets [] to get or set values in arrays and hashes.
For variables, just use the variable name to access or assign a value.
Examples
This gets the value at index 1 (second item) from the array.
Ruby
arr = [10, 20, 30]
puts arr[1] # Accessing the second element
This changes the third item in the array to 50.
Ruby
arr = [10, 20, 30]
arr[2] = 50# Setting the third elementputs arr.inspect
This gets the value for the key "name" from the hash.
Ruby
h = {"name" => "Alice", "age" => 25}
puts h["name"] # Accessing value by key
This updates the "age" key to 26 in the hash.
Ruby
h = {"name" => "Alice", "age" => 25}
h["age"] = 26# Setting a new value for keyputs h.inspect
Sample Program
This program shows how to get and change values in an array and a hash.
Ruby
arr = [5, 10, 15]
puts"Original array: #{arr.inspect}"# Access the first element
first = arr[0]
puts"First element: #{first}"# Change the second element
arr[1] = 20puts"Updated array: #{arr.inspect}"# Using a hash
person = {"name" => "Bob", "city" => "NY"}
puts"Original hash: #{person}"# Access nameputs"Name: #{person["name"]}"# Set new city
person["city"] = "LA"puts"Updated hash: #{person}"
OutputSuccess
Important Notes
Remember, array indexes start at 0 in Ruby.
When setting a value, if the key or index does not exist, Ruby will add it (for hashes) or expand the array.
Summary
Use square brackets [] to access or set values in arrays and hashes.
Accessing means reading a value; setting means changing or adding a value.
Indexes in arrays start at zero, keys in hashes can be strings or symbols.
Practice
(1/5)
1. Which of the following correctly accesses the first element of an array arr in Ruby using indexing?
easy
A. arr[1]
B. arr.first()
C. arr[0]
D. arr.get(0)
Solution
Step 1: Understand array indexing in Ruby
Ruby arrays start at index 0, so the first element is at arr[0].
Step 2: Check each option
arr[0] correctly accesses the first element. arr[1] accesses the second element. arr.first() is a method call that also returns the first element but is not using square brackets. arr.get(0) is invalid syntax in Ruby.
Final Answer:
arr[0] -> Option C
Quick Check:
First element index = 0 [OK]
Hint: Remember: array indexes start at zero in Ruby [OK]
Common Mistakes:
Using index 1 for first element
Using method calls instead of brackets
Trying to use non-existent methods like get()
2. Which syntax correctly sets the value of key :name to "Alice" in a Ruby hash person?
easy
A. person[:name] == "Alice"
B. person.name = "Alice"
C. person["name"] = Alice
D. person[:name] = "Alice"
Solution
Step 1: Understand hash key assignment syntax
To set a value in a hash, use square brackets with the key and assign with =.
Step 2: Analyze each option
person[:name] = "Alice" correctly sets the key :name. person.name = "Alice" is invalid for hashes. person["name"] = Alice misses quotes around Alice, causing error. person[:name] == "Alice" is a comparison, not assignment.
Final Answer:
person[:name] = "Alice" -> Option D
Quick Check:
Use brackets and = for hash assignment [OK]
Hint: Use brackets and = to set hash values [OK]
Common Mistakes:
Using dot notation for hashes
Forgetting quotes around string values
Using == instead of = for assignment
3. What is the output of the following Ruby code?
arr = [10, 20, 30]
arr[1] = 50
puts arr.inspect
medium
A. [10, 50, 30]
B. [10, 20, 30]
C. [50, 20, 30]
D. Error
Solution
Step 1: Understand array element assignment
Assigning arr[1] = 50 changes the element at index 1 (second element) to 50.
Step 2: Check the array after assignment
Original array is [10, 20, 30]. After assignment, it becomes [10, 50, 30].
Final Answer:
[10, 50, 30] -> Option A
Quick Check:
Index 1 value changed to 50 [OK]
Hint: Index 1 means second element in array [OK]
Common Mistakes:
Thinking index 1 is first element
Expecting original array unchanged
Confusing assignment with comparison
4. Identify the error in this Ruby code that tries to set a hash value:
person = {name: "Bob"}
person.name = "Charlie"
medium
A. Missing quotes around "Charlie"
B. Using dot notation to set hash value causes error
C. Hash keys must be strings, not symbols
D. person variable is not defined
Solution
Step 1: Understand hash value setting syntax
Hashes use square brackets with keys to access or set values, not dot notation.
Step 2: Analyze the code
person.name = "Charlie" tries to use dot notation, which is invalid for hashes and causes an error.
Final Answer:
Using dot notation to set hash value causes error -> Option B
Quick Check:
Use brackets, not dot, for hash assignment [OK]
Hint: Use brackets, not dot, to set hash values [OK]
Common Mistakes:
Using dot notation for hashes
Confusing symbols and strings for keys
Assuming variable is undefined
5. Given the nested hash data = {user: {name: "Eve", age: 28}}, how do you change the user's age to 30?
hard
A. data[:user][:age] = 30
B. data[:user.age] = 30
C. data.user[:age] = 30
D. data[:user][age] = 30
Solution
Step 1: Access nested hash keys with brackets
To access nested values, use square brackets for each key: data[:user][:age].
Step 2: Assign new value to nested key
Set data[:user][:age] = 30 to update the age.
Final Answer:
data[:user][:age] = 30 -> Option A
Quick Check:
Use chained brackets for nested hashes [OK]
Hint: Chain brackets for nested hash keys [OK]
Common Mistakes:
Using dot notation for hashes
Missing quotes or symbols for keys
Using undefined variables as keys
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.