How can I do this in Ruby?
For example, in Ruby:
a = [1,2,3]
h= {vara: a}
p h[:vara] # [1,2,3]
a = [42,43,44]
p h[:vara] # [1,2,3] - still the same
Hash shows the same results (array [1,2,3]
) although I have changed a
.
As for this example, I want hash shows different results when a
changes:
a = [1,2,3]
h= {vara: a}
p h[:vara] # [1,2,3]
a = [42,43,44]
p h[:vara] # [42,43,44] - changed!
How to do it in general case - variable nor container (hash in above example) isn't specified?
For example, in the C++ you can use reference:
#include <iostream>
#include <stdlib>
int main(void)
{
int a = 10;
int &var = a;
std::cout<< "\na = "<< a <<"\n";
a = 42;
std::cout<< "\nnew var = " << var << "\n"; //
std::cout<< "\nnew a = " << a << "\n";
system("pause");
return 0;
}
will produce:
a = 10
new var = 42
new a = 42
Is there something like this in the Ruby?