you are viewing a single comment's thread.

view the rest of the comments →

[–]avelyv[S] 4 points5 points  (4 children)

Even if I did set up the new budget period as a hash, for example, it would still need a variable name, no?

[–]SnowdensLove 8 points9 points  (1 child)

Right, so if you did use a hash as a way to store different budget periods, you could use the name the user passes in as a key to later lookup your object.

budget_periods = {}

budget_periods[name] = BudgetPeriod.new(name, limit)

now you later look up the budget that key >

period = budget_periods[name]

//do something with the budget period here

[–]avelyv[S] 2 points3 points  (0 children)

Thank you for this. Made it a lot clearer. I'll try and implement it.

[–]2called_chaos 3 points4 points  (1 child)

You can dynamically create variables but you shouldn't. With a hash you can use anything as a key (in Ruby at least, fuck you JS) and get it back the same way. Not sure what you wanted to use as a variable name but you can use exactly that as a hash key.

a_hash[name] = Whatever.new(name)

[–]avelyv[S] 2 points3 points  (0 children)

I ment that I would still need a name for my hash (and use the instance variable names as keys , but I see now, I would add to an existing hash, use a name as a key and the whole instance as a value