2

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".

4
  • Since Ruby v1.8 it has not been possible to create local variables dynamically. Commented May 4, 2020 at 22:24
  • The items in your array will not be ["a", "b", "c", "d"]. They will be ["a", "a's value", "b", "b's value", ...] Commented May 4, 2020 at 22:25
  • 2
    Do try and steer far, far away from global variables in Ruby. These are prefixed with $ 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 @vars instance variable, then manipulate that using an object instance method like create or, even better, []=(name, value). How this is different from a Hash, I don't know. Commented May 4, 2020 at 23:23
  • It's worth noting that Ruby can already create actual variables with instance_variable_set and local_variable_set. You don't need to fake it. Commented May 4, 2020 at 23:24

2 Answers 2

4

A Hash is used to store pairs of items. It is faster, and guarantees names will be unique.

$vars = {}
def create(name, value)
  $vars[name] = value
end

Then deleting is trivial.

$vars.delete(name)

As is finding the value.

value = $vars[name]

Hashes remember the order in which keys were added. $vars.keys will return an Array of the names in the order they were added. Then you can use find_index to get where it appears in the list of keys.

index = $vars.keys.find_index(name)

Aside from letting the user know the order in which variables are declared, this isn't of much use. Variables don't have an order.


If you wanted to do this with arrays, first we'd fix create. It's storing both the key and the value in the same Array.

create(a, 23)
create(b, 42)

# $var = ['a', 23, 'b', 42]

Instead, store each pair in its own array.

def create(name, value)
  $vars << [name, value]
end

create(a, 23)
create(b, 42)

# $var = [['a', 23], ['b', 42]]

Then to delete, search just the first elements of each pair using index. Then delete that index.

def delete(name)
  idx = $vars.map(&:first).index(name)
  $vars.delete_at(idx) if idx

  return idx
end

Finding the value of a name would work similarly. Find the index of the matching name, look up that pair, return its second element (the value).

def find(name)
  idx = $vars.map(&:first).index(name)
  pair = $vars[idx]
  return pair[1]
end

But don't do this, use a Hash.

Sign up to request clarification or add additional context in comments.

Comments

0
$vars = ["name", "value", "item"]
index = $vars.index("name")
$vars.delete_at(index)

Sources:

  1. Duplicate?
  2. Editor
  3. Documentation

Although if this is rails, is it really a good idea to use a GLOBAL variable?

4 Comments

@theTinMan The links describe what I link...and editor...the documentation for index and a source that might be a duplicate..whats the issue?
The issue is, links rot then break. When they break, what will this tell us? Instead, you're supposed to summarize the important point of the link that you're trying to make, appropriately attributed, so when the link breaks, the answer continues to make sense. We answer questions, not as much for the person asking, but for those who are looking for a solution in the future. Imagine your answer with broken links. How useful would it be?
@theTinMan understood.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.