SwitchUp SwitchUp Ranked Best Coding Bootcamps 2025

Why can't I use dot notation to access Ruby Hash Object elements

Altcademy Team wrote on 7 February 2018

I have a ruby Hash Object retrieved from the postgresql database.
hash = {"foo": bar} puts hash.foo => NoMethodError: undefined method 'foo' for {"foo": bar}:Hash
Please help!!

Ruby Hash objects do not support dot notation, that is a JavaScript feature.
To access your properties in your ruby Hash, use square brackets notation.
hash = {"foo": "bar"} puts hash["foo"] => "bar"

Trusted by

Students and instructors from world-class organizations

Join the upcoming Cohort #111

Enroll for March 2nd, 2026