OpenStruct is a Ruby class that allows you to create data objects with arbitrary attributes that can be set and read dynamically, without defining a class beforehand.
Click to reveal answer
beginner
How do you create a new OpenStruct object with attributes?
You create it by calling OpenStruct.new and passing a hash of key-value pairs. For example: person = OpenStruct.new(name: 'Alice', age: 30).
Click to reveal answer
beginner
How can you add or change attributes on an existing OpenStruct object?
You can assign new attributes or change existing ones by simple dot notation, like person.city = 'New York' or person.age = 31.
Click to reveal answer
beginner
What happens if you try to access an attribute that was never set on an OpenStruct object?
It returns nil instead of raising an error, because OpenStruct allows dynamic attributes that may or may not exist.
Click to reveal answer
intermediate
Why might you use OpenStruct instead of a regular Ruby class?
OpenStruct is useful when you want a quick, flexible object to hold data without writing a full class. It’s great for prototyping or when attribute names are not known in advance.
Click to reveal answer
How do you create an OpenStruct object with a name attribute set to 'Bob'?
AOpenStruct.set(name: 'Bob')
BOpenStruct.create('name' => 'Bob')
COpenStruct('name' => 'Bob')
DOpenStruct.new(name: 'Bob')
✗ Incorrect
You create an OpenStruct object by calling OpenStruct.new with a hash of attributes.
What will person.height return if height was never set on an OpenStruct object?
Anil
B0
CAn error
Dfalse
✗ Incorrect
Accessing an unset attribute on OpenStruct returns nil instead of an error.
How do you add a new attribute 'city' with value 'Paris' to an existing OpenStruct object named person?
Aperson.add(:city, 'Paris')
Bperson.city = 'Paris'
Cperson[:city] = 'Paris'
Dperson.set_city('Paris')
✗ Incorrect
You add or change attributes on OpenStruct objects by simple dot notation.
Which Ruby library must you require to use OpenStruct?
Arequire 'ostruct'
Brequire 'open_struct'
Crequire 'struct'
Drequire 'dynamic_object'
✗ Incorrect
You must require 'ostruct' to use the OpenStruct class.
Why might OpenStruct be less efficient than a regular Ruby class?
ABecause it requires network access
BBecause it compiles code at runtime
CBecause it uses method_missing and dynamic method creation
DBecause it stores data in a database
✗ Incorrect
OpenStruct uses dynamic method handling which is slower than fixed methods in regular classes.
Explain how OpenStruct allows you to create objects with flexible attributes in Ruby.
Think about how you can add or read properties without a class.
You got /3 concepts.
Describe a situation where using OpenStruct would be more convenient than defining a full Ruby class.
When you want to store data fast without writing extra code.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of Ruby's OpenStruct?
easy
A. To improve performance of Ruby programs
B. To create objects with dynamic properties without defining a class
C. To enforce strict typing on object properties
D. To replace arrays with key-value pairs
Solution
Step 1: Understand what OpenStruct does
OpenStruct allows creating objects where you can add or change properties anytime without predefining a class.
Step 2: Compare options with OpenStruct's purpose
Options A, C, and D do not describe OpenStruct's main use. It is not about performance, strict typing, or replacing arrays.
Final Answer:
To create objects with dynamic properties without defining a class -> Option B
Quick Check:
OpenStruct = dynamic properties [OK]
Hint: OpenStruct = flexible object with changeable properties [OK]
Common Mistakes:
Thinking OpenStruct improves speed
Confusing OpenStruct with strict typing
Assuming OpenStruct replaces arrays
2. Which of the following is the correct way to create an OpenStruct object with a property name set to "Alice"?
easy
A. person = OpenStruct.new(name: "Alice")
B. person = OpenStruct(name = "Alice")
C. person = OpenStruct{name: "Alice"}
D. person = OpenStruct.new("name", "Alice")
Solution
Step 1: Recall OpenStruct initialization syntax
OpenStruct.new accepts a hash with symbol keys to set initial properties.
Step 2: Check each option's syntax
person = OpenStruct.new(name: "Alice") uses correct syntax with symbol key and value. person = OpenStruct(name = "Alice") uses wrong method call. person = OpenStruct{name: "Alice"} uses braces instead of parentheses. person = OpenStruct.new("name", "Alice") uses positional arguments which OpenStruct.new does not support.
Final Answer:
person = OpenStruct.new(name: "Alice") -> Option A
Quick Check:
OpenStruct.new(symbol: value) [OK]
Hint: Use OpenStruct.new with symbol keys for properties [OK]
Common Mistakes:
Using assignment inside parentheses
Using braces instead of parentheses
Using positional arguments instead of hash
3. What will be the output of the following Ruby code?
require 'ostruct'
person = OpenStruct.new
person.age = 30
puts person.age
OpenStruct allows adding properties dynamically by assignment, so person.age = 30 sets the age property to 30.
Step 2: Output the property value
Using puts person.age prints the value 30 stored in the age property.
Final Answer:
30 -> Option C
Quick Check:
Dynamic property assignment = 30 [OK]
Hint: Assign properties directly, then access them normally [OK]
Common Mistakes:
Expecting nil for uninitialized property
Thinking dynamic assignment causes error
Confusing with normal Struct behavior
4. Identify the error in this code snippet using OpenStruct:
require 'ostruct'
person = OpenStruct.new
person[:name] = "Bob"
puts person.name
medium
A. person.name is undefined because name was never set
B. Missing parentheses in OpenStruct.new
C. Cannot use puts with OpenStruct properties
D. OpenStruct does not support []= for setting properties
Solution
Step 1: Check how properties are set in OpenStruct
OpenStruct properties are set by dot notation (e.g., person.name = "Bob"), not by bracket notation.
Step 2: Analyze the code behavior
Using person[:name] = "Bob" tries to use []= method which OpenStruct does not support, causing an error.
Final Answer:
OpenStruct does not support []= for setting properties -> Option D
Quick Check:
Use dot notation, not []= [OK]
Hint: Set properties with dot, not brackets [OK]
Common Mistakes:
Using []= to set OpenStruct properties
Assuming OpenStruct supports hash-like access
Ignoring error messages
5. Given a hash data = {name: "Eve", age: 28, city: "NY"}, how can you create an OpenStruct object and then add a new property country with value "USA" dynamically?
hard
A. person = OpenStruct.new(data); person.country = "USA"
B. person = OpenStruct.new; person = data; person.country = "USA"
C. person = OpenStruct.new(data); person[:country] = "USA"
D. person = OpenStruct.new; person.add(:country, "USA")
Solution
Step 1: Initialize OpenStruct with existing hash
Use OpenStruct.new(data) to create an object with properties from the hash.
Step 2: Add new property dynamically
Assign new property with dot notation: person.country = "USA".
Step 3: Check incorrect options
person = OpenStruct.new; person = data; person.country = "USA" overwrites person with hash, losing OpenStruct. person = OpenStruct.new(data); person[:country] = "USA" uses []= which is invalid. person = OpenStruct.new; person.add(:country, "USA") uses a non-existent method add.
Final Answer:
person = OpenStruct.new(data); person.country = "USA" -> Option A
Quick Check:
Initialize with hash + dot assign new property [OK]
Hint: Initialize with hash, add properties by dot assignment [OK]
Common Mistakes:
Overwriting OpenStruct with hash
Using []= to add properties
Calling non-existent methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.