Ruby convert Object to Hash

前端 未结 17 1693
滥情空心
滥情空心 2020-12-12 10:35

Let\'s say I have a Gift object with @name = \"book\" & @price = 15.95. What\'s the best way to convert that to the Hash {na

17条回答
  •  不知归路
    2020-12-12 11:05

    You should override the inspect method of your object to return the desired hash, or just implement a similar method without overriding the default object behaviour.

    If you want to get fancier, you can iterate over an object's instance variables with object.instance_variables

提交回复
热议问题