Forbes magazine logo Ranked Best Coding Bootcamps 2023

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

Imperial College London
Carnegie Mellon University
City University of Hong Kong
Hack Reactor
Cisco Meraki
University of Oxford
Swift
Bazaarvoice
Waterloo
Uber
AtlanTech
Tumblr
Boston College
Bombardier Aerospace
University of St. Andrews
New York University
Minerva Schools at KGI
Merrill Lynch
Riot Games
JP Morgan
Morgan Stanley
Advanced Placement®
Google
KPMG
The University of Hong Kong
University of Toronto
SCMP
Moat
Zynga
Hello Toby
Deloitte
Goldman Sachs
Yahoo
HSBC
General Assembly
Tesla
McGill University
Microsoft

Join the upcoming Cohort #89

Enroll for May 6th, 2024