-1
{"string_1_0" => "1.0","$string_1"=>2}
puts $string_1
  1. In the above mentioned the ruby hash, now i want to turn hash key into local variable.
  2. Use hash key string as it is to turn into local variable.
  3. it should run with ruby instead of jruby
  4. Expected output:
2
  • This is what you need: https://stackoverflow.com/questions/18552891/how-to-dynamically-create-a-local-variable – razvans Aug 24 '21 at 11:47
  • Why is your question tagged as `ruby-on-rails`? What does it have to do with rails? Are you hoping to access these variables in a rails view, or is the fact that you're using rails irrelevant? – Tom Lord Aug 24 '21 at 11:50
  • Likewise for `rubygems` -- What does your question have to do with 3rd party ruby libraries? – Tom Lord Aug 24 '21 at 11:58
  • Why are you using a version of Rails that hasn't been maintained for years, and has numerous unpatched security holes? – Jörg W Mittag Aug 24 '21 at 12:25

2 Answers2

1

You cannot define a local variable with a full stop (.) character in ruby. That is not valid syntax.

(eval):2: unexpected fraction part after numeric literal
string_1.0 = "1.0"

Additionally, you cannot dynamically define local variables. There are various workarounds to sort-of achieve this, however, fundamentally I think you are asking an XY problem.

For example, have you considered using an OpenStruct, or passing this hash as locales when rendering a template, or instead dynamically setting instance variables?

Tom Lord
  • 27,404
  • 4
  • 50
  • 77
0

As Tom stated above, you appear to be asking an XY problem and could likely solve the fundamental/core problem in a much more efficient manner. More detail as to exactly what you're trying to accomplish and why you feel the need to convert a key:value to a local variable would be helpful.

That being said, there are a lot of potential alternatives but most would require that you deal with your naming syntax first. As Tom stated above, you cannot include a dot in variable names. Once that issue was dealt with, the closest thing I can think of to what you're asking for would be to define a method instead of a local variable. That might look something like this:

hash = {"string_1_0" => "1,0", "string_2_2" => "2,2"}
hash.each {|key, value| define_method (key) {value}}
    
string_1_0  #=> 1,0
string_2_2  #=> 2,2

Again, just beware that these are methods and not local variables. As such, they will be accessible on a much more global scale which may or may not work for your application.

  • 1
    You don't need to use `eval` to define a method! Use `define_method`. – Tom Lord Aug 25 '21 at 14:32
  • You are correct good sir. I edited my answer. –  Aug 26 '21 at 15:31
  • 1
    How can i do it for local and global variables like {"$string_2" => 2,"string_1_0"=>"1.0"} – Basa Sai ram Sep 04 '21 at 17:53
  • Here's another alternative: ```hash = {"$string_1_0" => "1,0", "string_2_2" => "2,2"} hash.each {|key, value| eval "#{key} = \"#{value}\""}``` Using ```eval``` is typically considered bad practice most of the time and many people insist that its always bad practice. I know from personal experience though that it does have its applications, so bad practice or not, its good to know that its an available approach. Just read up on using it though so you're at least aware of the risks. –  Sep 05 '21 at 05:09