I have some code that finds a keyword in an array (clues) and returns its position, I am trying to take this result and execute it as a puts command. This search is necessary as I will not always know ‘Software includes’ position in the array
My code below just simple outputs “clues[2]” but I want to actually execute puts clues[2] as though I had typed it in so my output would be “Software included”.
Is there a way to make this workable?
clues = Array.new
clues << 'Power supply type'
clues << 'Slots'
clues << 'Software included'
Var100 = clues.rindex('Software included')
Var101 = "clues[#{Var100}]"
command_store = Array.new
command_store << lambda {puts "clues[#{Var101}]" }
You’re complicating things too much. For example, your
putscommand in a lambda is equivalent to this:With a little debug printing you’ll be able to make it work. Here’s what I think you want to get:
Although this exact code doesn’t make any sense to me (who gets value’s index only to use that index to print the value), I understand that it may be an over-simplified example.
Hints:
[]toArray.newat all times.