-1

I want to create a new variable from array elements. This is a very bad code and does not work:

(1..5).each {|x| print "step " + x.to_s + ": "; name_of_variable_+_x  = gets.chomp}

but I want to understand the meaning of what I want to do.

sawa
  • 165,429
  • 45
  • 277
  • 381
awar
  • 61
  • 8
  • 2
    It sounds like you want to take an array like `[ "a", "b", "c" ]` and define variables from it as though you had done `item_0 = "a"; item_1 = "b"; item_2 = "c"`. The question, then, is *why* do you want to do this? What problem are you trying to solve by doing this? I'm compelled to ask this because what you're trying to do is almost certainly the wrong solution to whatever problem you're trying to solve. – Jordan Running Jan 15 '16 at 19:57
  • Instead of dynamically creating 5 variables, you are better off creating an array of 5 elements. – Wand Maker Jan 15 '16 at 20:00
  • Each variable must be contain 5 different input user. The problem is that i can't to know how will the new variables – awar Jan 15 '16 at 20:05
  • Old-timers have told me that in v1.8 you could create a local variable using `eval`. In 1.9+ it is not possible to create a local variable dynamically. – Cary Swoveland Jan 15 '16 at 20:42
  • @CarySwoveland It is possible, but you need to do it on a binding. – sawa Jan 15 '16 at 21:08
  • @CarySwoveland It's in my answer. – sawa Jan 15 '16 at 21:12

2 Answers2

2

This is the case where you use a variable with an Array.

vars = []
(1..5).each do |x|
  vars[x] = gets.chomp
  puts "step #{x}: #{vars[x]}"
end

If you really want to define a variable, then you must use eval. This is a terrible idea because you will be using a very dangerous feature (eval) to implement a very silly idea (defining number-based variable).

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
  • So, im happy, you're italian like me. I need to create 5 new variable that must contain five different user input. What your best solution? – awar Jan 15 '16 at 20:00
  • Even if one defines variable using `eval`, one has to access it using another `eval` - so it seems from this thread [Define var with eval](http://stackoverflow.com/questions/14404198/ruby-1-9-3-define-var-with-eval) – Wand Maker Jan 15 '16 at 20:07
  • Simone you right. I not really need of a variable. I can use the element in an array. Thanks. – awar Jan 15 '16 at 20:23
1
  • You can define local variables dynamically on binding:

    b = binding
    b.local_variable_set("name_of_variable_#{x}", gets.chomp)
    

    but you would then have to keep carrying that b around when you want to get the value, and is not convenient.

  • A slightly better way is to use an instance variable, which does not require you to use a binding:

    instance_variable_set("@name_of_variable_#{x}", gets.chomp)
    
  • But when you have a sequence of values, especially when they are numbered, there is no reason to keep them each in an individual variable. You should just have a single array that keeps all information:

    variables = Array.new(5){|x| print "step #{x + 1}: "; gets.chomp}
    
sawa
  • 165,429
  • 45
  • 277
  • 381