A
A
Alexander2016-01-15 16:15:53
ruby
Alexander, 2016-01-15 16:15:53

Does Ruby have variable variables like PHP does?

In php there is a concept like variable variables

$a=5;
$b='a';
echo $$b => 5

does something similar exist in ruby?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2016-01-15
Protko @Fesor

no. it is possible to operate on variables through the functions instance_variable_get/instance_variable_set
However, as in the case of PHP, this is bad practice. Use hash maps.

E
Eugene Burmakin, 2016-01-15
@Freika

You can do something like this (in my opinion, in any language in general)

a = 1
b = a
puts a #=> 1
puts b #=> 1

V
vsuhachev, 2016-01-15
@vsuhachev

In ruby, almost any meta-information can be accessed from the program. The local variable can be accessed through the methods of the Binding class

a = 1
b = :a
puts binding.local_variable_get(b)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question