Usually instance variables are held within an instance of a class that your code instantiates and populates with data pointed to by it’s instance variables. You can pass the instance’s reference and then the methods can access the ivars via accessor methods (ie “getter” method.)
When your code is done with this data, you dispose of the instance (by setting it to nil
,) and garbage collection will dispose of all the mutable objects that the instance referenced. No need for a “hard purge”.
But Ruby has several ready built features you can use. (Yes I’ve told you of these in the past.)
You can code your own data class.
class Data
attr_accessor(:name,:type,:material,:width,:height)
end
properties = Data.new
properties.name= "Garage Door"
# ... etc.
some_method_call(properties)
You can use Structs. You can use a Hash.
Or probably simplest would be an OpenStruct.
require 'ostruct'
properties = OpenStruct.new(
name: "Garage Door",
material: "Reinforced Metal",
type: "Segmented Overhead Rollup",
height: 9.feet,
width: 20.feet
)
some_method_call(properties)
Anyway, when you’re done with the data, just set the data object to nil
…
properties = nil
# GC will clean up automatically
Or, if you created the data object within a method, when the method ends, the data object would “go out of scope” and become unreferenced and get cleaned up by GC anyway, with no need to explicitly set it to nil
.
def create_garage_door
properties = OpenStruct.new(
name: "Garage Door",
material: "Reinforced Metal",
type: "Segmented Overhead Rollup",
height: 9.feet,
width: 20.feet
)
build_door_geometry(properties)
end
So, when create_garage_door()
returns it’s local reference properties
goes out of scope and will get cleaned up by GC (as long as build_door_geometry
didn’t assign a persistent reference to it.)