is it correct to add self like in this case in ruby on rails -
i trying make available repost post in app...i see in youtube video code..
class pin < activerecord::base validates_presence_of :bio belongs_to :user mount_uploader :image, pictureuploader def repost repost_pin = self.dup repost_pin.save end
is correct use self here? , possible change this:
def repost dub end
in end repost called on instance variable @post(@post = post.find(params[:id]) ) .... maybe misunderstanding here...anyone can help?
you can skip self
when clear context reading current instance's attribute or calling method.
when writing should more careful this:
def write test = 'test' end
will create local variable test
if there attribute same name. alternatively, this:
def write self.test = 'test' end
will assign value current instance's attribute named test
.
in example skip self
since dup
method of object
, therefore available in current context valid identifier:
def repost repost_pin = dup repost_pin.save end
that said, not error explicitly use self
e.g., mark intent use object's attribute or method. ruby style guide not recommend though.
Comments
Post a Comment