using gets.chomp twice in a one line call to a nested hashmap - ruby

316 Views Asked by At

what is the best way of using gets.chop for the following example?

user = {}
user["list"] = [ {gets.chomp => {gets.chomp.delete(' ') => rand(1000000000000)} } ]

I can think of:

a = gets.chop ; b = a.delete(' ') ; user["list"] = [ {a => {b => rand(1000000000000)} } ]

but perhaps there is a better way?

any ideas? Can I do it without creating the vars a & b ?

1

There are 1 best solutions below

3
On BEST ANSWER

You will have to set a variable to use the input in two different places. ALthough it can be compacted into the following :

user["list"] = [ {a=gets.chomp => {a.delete(' ') => rand(1000000000000)} } ]