When I was recently working in one of the client project, I had to communicate with external server to store records from app, that means I would get hash from our app which I had to convert to pure query and send it to external server for storing. If you have worked with queries previously then you must know that keys and values must be separated for insert operations like mariadb react/rails activerecord sql sql sql (first_name, last_name, email) (John, Doe, john@email.com) INSERT INTO users VALUES I could convert attributes to hash easily using to get the format . as_json {"first_name"=>"John", "last_name"=>"Doe", "email"=>"john@email.com"} { => , => , => } "first_name" "John" "last_name" "Doe" "email" "john@email.com" But I had to extract keys and values separately so that attributes can be accurately formatted and ready for insert and update operations. Let me show you how I extracted keys and values from the hash and formatted them as required for the operations. Let's assume we have: user = { => , => , => } "first_name" "John" "last_name" "Doe" "email" "john@email.com" If we only want Extract single key or value email rails-console // For key user.extract!("email").keys // For value user.extract!("email").values user['email'] # ["email"] # with extract # ["john@email.com"] # simply # "john@email.com" If we want and but not Extract multiple keys or values first_name last_name email rails-console // keys user.extract!(*[ , ]).keys // values user.extract!(*[ , ]).values For "first_name" "last_name" # [ , ] "first_name" "last_name" For "first_name" "last_name" # [ , ] "John" "Doe" Extract all keys or values rails-console // keys .keys # ["first_name", "last_name", "email"] // . # ["John", "Doe", "john@email.com"] For user For values user values Do you know more elegant or alternative way to extract keys and values from hashes? Please enlighten and guide us with your precious comment below if you do. Previously published at https://thedevpost.com/blog/extract-key-or-value-from-hash-in-ror/