Search code examples
rubyhashmapchomp

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


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 ?


Solution

  • 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)} } ]