samedi 4 juillet 2020

How to correctly check for nested values in ruby

I have a ruby hash that looks like this:

payload = {
  "fields" => {
    "publishedDate" => {
      "de"=>"2019-05-02T00:00+02:00"
    }
  }
}

So now I want to access the date. How would I efficiently and correctly write an if statement to check if the date exists?

I'm currently doing something like this but it somehow feels like it is too much code:

payload["fields"] && payload["fields"]["publishedDate"] && payload["fields"]["publishedDate"].first && payload["fields"]["publishedDate"].first.last

Thank you for helping me out!

Aucun commentaire:

Enregistrer un commentaire