ruby - using a string (from gets) directly as a variable name -
starting out towers of hanoi assignment, have
a = [6,5,4,3,2,1] b = [] c = [] puts "type a, b, or c" = gets.chomp # user types lower-case popped = from.pop
now fails because pop not string method.
so other than
if == popped = a.pop elsif == b popped = b.pop
, there nice ruby shortcut pop intend?
you can use eval
:
a = [6,5,4,3,2,1] b = [] c = [] puts "type a, b, or c" = gets.chomp popped = eval(from).pop
but eval
typically seen bad idea security, performance, , debugging reasons.
Comments
Post a Comment