Convert object to hash ruby

Spread the love

Get Ruby Object (that you need to convert to hash) into one variable.

=> user = User.first

#<User id: 1, name: “Test", email: “[email protected]", updated_at: "2019-01-23 06:03:20”> 

Below method will convert your Ruby Object into hash.

=> user.as_json

{"id"=>1, "name"=>"Test", "email"=>"[email protected]", "updated_at"=>Wed, 23 Jan 2019 06:03:20 UTC +00:00}

Find below link for details about Hash:

https://ruby-doc.org/core-2.2.0/Hash.html

https://www.learningcontainer.com/rspec-for-ruby-on-rails/
Learn About Test cases