How to create a variable name from the value of a string in Ruby on Rails? How to create a variable name from the value of a string in Ruby on Rails? ruby ruby

How to create a variable name from the value of a string in Ruby on Rails?


This instance_variable_set("#{foo}", "cornholio") needs to read instance_variable_set("@#{foo}", "cornholio")

Based on this post. Just tried it in my irb for Ruby 1.93; the post is from 2009.


In Ruby, instance variable names always have to start with an @ sigil.


I was looking for the answer for the same question but with an other thought. Because other people might ending up here looking for my answer here my question and solution:

I want to cal t(key, interpolation_var: value) in a method. where interpolation_var: is different per translation.solution(I have removed all non important information so only the solution to the problem is available. that's why the method looks unusefull :) ):

def some_function(key, interpolation_var, value)  t(key, :#{interpolation_var} => value)end