lundi 22 février 2021

Hash method get on ruby

I need to get some values for a hash but, I don't know how can I do it! My data:

data = {
          name: "Pedro Álvares",
          age: 35,
          sensitive_data: {
            cpf_cnpj=>27046645678, 
            fantasy_name: "I have the power"
            }
        }

I search for ruby methods and find the method values_at, but this method gets only the first_data like name, age. If I use:

name = data.values_at(:name)

the field returned is Pedro Álvares but if I try use:

fantasy_name = data.values_at(:fantasy_name)

the data returned is nil.

How can I get the field without using data[:sensitive_data][:fantasy_name]?

Aucun commentaire:

Enregistrer un commentaire