Adam Lee Adam Lee - 1 month ago 6
Ruby Question

About the following code in Ruby?

I am reading a piece of code as follows,

u = account.generate_user
{
id: u.id,
name: u.name
}


Could you let me know what does the above snippet does? Does it do the following?

u = account.generate_user
account.id = u.id
account.name = u.name


Is there any reference to this usage?

Answer

You first generate an object (u), and then return a hash, with values being method calls on that object.

Does it do the following?

u = account.generate_user account.id = u.id account.name = u.name

Nope. In the hash you are not accessing account's attributes/methods (id and name), but of the newly created object u.

What is u depends on account.generate_user return value.