Ruby : Associative Arrays

前端 未结 3 875
难免孤独
难免孤独 2020-12-13 23:57

Does Ruby on rails have associative arrays?

For eg:

   a = Array.new
   a[\"Peter\"] = 32
   a[\"Quagmire\"] = \'asdas\'

What is

3条回答
  •  余生分开走
    2020-12-14 00:13

    Use hashes, here's some examples on how to get started (all of these do the same thing, just different syntax):

    a = Hash.new
    a["Peter"] = 32
    a["Quagmire"] = 'asdas'
    

    Or you could do:

    a = {}
    a["Peter"] = 32
    a["Quagmire"] = 'asdas'
    

    Or even a one liner:

    a = {"Peter" => 32, "Quagmire" => 'gigity'}
    

提交回复
热议问题