Convert Hash Key to a Method

less than 1 minute read

Consider you have a Hash like this or you got a method which returns as a result.

hash = { name: 'Serdar', last_name: 'Doğruyol'}

hash.name # undefined method `name' for {:name;"Serdar", :last_name:"Doğruyol"}:Hash

Unfortunately when you try to access the hash key as you would in a normal object you get an ‘undefined method’ error. Luckily we can solve this easily with some Ruby magic :)

require 'ostruct'
hash = { ad: 'Serdar', soyad: 'Doğruyol'}
hash = OpenStruct.new hash
hash.ad # Serdar

Now you can use your hash as an object and access the keys with method calls.

Happy hacking <3

Leave a Comment