0

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 ?

beoliver
  • 5,579
  • 5
  • 36
  • 72

1 Answers1

2

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

Kassym Dorsel
  • 4,773
  • 1
  • 25
  • 52
  • For some reason I was thinking that if I used `a=gets.chomp` inside the dictionary, the value of `gets.chomp` would not be saved... but no, this works... I just wish there was a way to do this without using a variable. – beoliver Jan 31 '12 at 00:36
  • If you could input it when you run the script from command line (`ruby myscript.rb input`) you could then access the input this way : `user["list"] = [ {ARG[0].chomp => {ARG.shift.chomp.delete(' ') => rand(1000000000000)} } ]` – Kassym Dorsel Jan 31 '12 at 00:41
  • interesting! I will have a play with that. I was thinking of having something like `.storevariable` so `gets.chomp.storevariable` is saved as `storevariable` which can then be edited... – beoliver Jan 31 '12 at 00:53