Home > Net >  What is the object method in Ruby
What is the object method in Ruby

Time:12-27

I found a piece of action code in a helper, its purpose is to change the API content of render JSON in the controller:

class Users::RefDecorator < Draper::Decorator
    delegate_all

    def ava
      object.user.ava
    end

User is another model, and ava is its column. But what does the "object." at the beginning mean?

CodePudding user response:

This decorator inherits from Draper::Decorator and object is a method defined on Draper::Decorator. Quote from the docs:

#objectObject (readonly)

Also known as: model

Returns the object being decorated.

It returns the object that was passed to as the first argument to the decorator's initialize.

CodePudding user response:

In case of Draper::Decorator as a parent class, object refers to the original object.

So I imagine that you wrote something like this:

user = SpecialUserClass.new

decorated_user = user.decorate
# or
decorated_user = Users::ReferrerProfileDecorator.decorate(user)

# now
decorated_user.object == user

Though by adding delegate_all you don't see it that often. From the original gem documentation:

methods have been made available on the decorator by the delegate_all call

CodePudding user response:

What is the object method in Ruby

There is no method named object in Ruby.

But what does the "object." at the beginning mean?

It is sending the message object to the implicit receiver self (in some other languages, this would be called "calling the method object on this").

This method seems to be defined by some library you are using. However, it is not one of the libraries that you tagged your question with, so it is hard to tell where it might come from.

  • Related