I'm making a sort of variable system in Ruby where a user can create a variable. Provided they give a variable name and contents, it adds the name and string given to an array, adding two more items to the array. Here's the code:
$vars = []
def create(name, value)
$vars << name
$vars << value
end
I also want to add a "delete" option where the user types delete("varName") and behind the scenes, it searches for an item with the same value in the $vars array and deletes it. Is there any way to do this? Also, is there any way to get the number of the item (e.g. items in the array are ["a", "b", "c", "d"] and the user wants to delete "d",and then the program returns 3, since that's the location of "d".
["a", "b", "c", "d"]. They will be["a", "a's value", "b", "b's value", ...]$and should only be used in exceptional circumstances. The Ruby way of doing this, presuming it's not some XY Problem that has a better solution, is to create a class with an@varsinstance variable, then manipulate that using an object instance method likecreateor, even better,[]=(name, value). How this is different from a Hash, I don't know.instance_variable_setandlocal_variable_set. You don't need to fake it.