Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a string as a variable at run time

Tags:

string

ruby

People also ask

How do you use string as a variable?

If you can depend on having Python >= version 3.6, then you have another attractive option, which is to use the new formatted string literal (f-string) syntax to insert variable values. An f at the beginning of the string tells Python to allow any currently valid variable names as variable names within the string.

Can a string be a variable?

A string is a type of value that can be stored in a variable.

What is runtime variable?

With runtime variables, you can define values that can be used as source values in Outbound Mappings or Conversation Variable Mappings in the same rule. This way the actual mappings can be made simpler, more readable and you can avoid repeating the same kind of configuration many times.

How do you store strings in a variable?

Here are some more examples of storing strings into variables: string lastName = "Smith"; string book = "Second Foundation"; string foo = "blah"; It is important to note that the variable name can be anything you want as long as it does not contain any spaces.


If you can forgive an @ sign in front of the variable name, the following will work:

variable_name = ... # determine user-given variable name
instance_variable_set("@#{variable_name}", :something)

This will create a variable named @whatever, with its value set to :something. The :something, clearly, could be anything you want. This appears to work in global scope, by declaring a spontaneous Object instance which binds everything (I cannot find a reference for this).

The instance_variable_get method will let you retrieve a value by name in the same manner.

instance_variable_get("@#{variable_name}")

You can use eval() for this provided that you've declared your variable first:

>> foo = []
>> eval("foo")[1] = "bar"
>> foo[1]
=> "bar"

Here are the docs.


Rather than do that directly, why not consider using a hash instead. The string would be the key, and the value you want to store would be the value. Something like this:

string_values = { }
some_string = params[:some_string]       # parameter was, say "Hello"
string_values[some_string] = 42
string_values                            # { 'Hello' => 42 }
some_number = string_values[some_string]
some_number                              # 42

This has a couple of benefits. First, it means you're not doing anything magic that might be hard to figure out later. Second, you're using a very common Ruby idiom that's used for similar functionality throughout Rails.


Now simply using instance_variable_set method, you can create a instance variable at runtime.

instance_variable_set('@' + 'users', User.all)

I don't mean to be negative, but tread carefully. Ruby gives you a lot of features for highly dynamic programming such as define_method, storing blocks as Proc objects to be called later, etc. Generally these are cleaner code and far safer. 99% of the time using eval() is a mistake.

And absolutely never use eval() on a string that contains user submitted input.