Technology Insights

Adding members to Ruby objects dynamically

If you want to dynamically add a member to a Ruby object, use the hash/array brackets and symbol (:user_id) as shown in the iRb session below. As you can see the dot notation will not work. bash >> d = AccessMode.find(1) => #<AccessMode id: 1, device_id: 31218, mode: "dialup", timestamp: "2007-12-25 15:52:55"> >> d[:user_id] =…