Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In object.initialize, is it better to use self. over @?

Tags:

ruby

There's a convention to reference an object's attributes over its instance variables, where possible. Practical Object-Oriented Design in Ruby says:

Always wrap instance variables in accessor methods instead of directly referring to variables...

This is shown with an example, which I've paraphrased:

class Gear
  attr_reader :chainring, :cog
  ...
  def ratio
    # this is bad
    # @chainring / @cog.to_f
    # this is good
    chainring / cog.to_f
end

The most common way I see to create a new object with an instance variable is this:

class Book
  attr_accessor :title
  def initialize(title)
    @title = title
  end
end

@title= directly accesses the instance variable title. Assuming we are following the the 'attribute over instance variable' convention, is it more appropriate to use self.title=, which would tell the object to send itself the message title=, thereby using the attribute write method, over the instance variable directly?

class Book
  attr_accessor :title
  def initialize(title)
    self.title = title
  end
end

The book talks about 'attribute over instance variable' with reference to reading an instance variable, but doesn't it also apply to writing?

like image 788
ben Avatar asked Jan 10 '14 04:01

ben


People also ask

What is self In __ init __?

The self in keyword in Python is used to all the instances in a class. By using the self keyword, one can easily access all the instances defined within a class, including its methods and attributes. init. __init__ is one of the reserved methods in Python. In object oriented programming, it is known as a constructor.

Why do we use self in Object Oriented Programming?

self is parameter in Instance Method and user can use another parameter name in place of it. But it is advisable to use self because it increases the readability of code, and it is also a good programming practice.

What happens if you don't use self in Python?

Don't use self when:you want to call an instance method normally; referencing a class attribute inside the class definition but outside an instance method; you are inside a static method.

What is the difference between using self and this?

The keyword self is used to refer to the current class itself within the scope of that class only whereas, $this is used to refer to the member variables and function for a particular instance of a class.


1 Answers

The book talks about 'attribute over instance variable' with reference to reading an instance variable, but doesn't it also apply to writing?

Yes, it also applies to writing. However, the initialize method is special because it is responsible for setting up the object. When you use a setter method, you do that because the setter might be doing some extra work (e.g. attribute-setters in Rails). In an initializer, you usually don't want to have any side effects, so you access instance variables directly.

like image 131
phoet Avatar answered Sep 29 '22 10:09

phoet