-2

If I create a class like this:

class Player

  def initialize(position, name)
    @position = position
    @name = name
  end

end

Isn't that setting the name to an instance variable? if so, why would I need to write a setter like this

 class Player

  def initialize(position, name)
    @position = position
    @name = name
  end

  def name=(name)
    @name = name
  end

 end

Basically when is it necessary to write getters in a class?

Bitwise
  • 8,021
  • 22
  • 70
  • 161
  • 1
    Your examples are all about *setters*, but your "basically" summary talks about *getters*. Is that a typo, or intentional? – user513951 Jan 04 '17 at 04:13

3 Answers3

1

initialize sets attributes during the initialization of a new object.

keeper = Player.new('goalkeeper','Shilton').

What if you want to update an attribute of keeper after initialzation? Well you'll need to use your ordinary setter method:

def name=(name)
  @name = name
end

like so:

keeper.name = 'Banks'

If you don't have a setter method defined for Player instances, then you can't do this. Similarly for getter methods. Also be aware you can refactor your code by using attr_accessor like so:

class Player
  attr_accessor :name, :position

  def initialize(position, name)
    @position = position
    @name = name
  end    
end
Community
  • 1
  • 1
Sagar Pandya
  • 9,323
  • 2
  • 24
  • 35
1

Getters and setters job is to provide you a quick implementation of read and write of instance variables that you define in your constructor:

class Player
  attr_accessor :name, :position
  def initialize(position, name)
    @position = position
    @name = name
  end
end

you can also user attr_reader(for getters) and attr_writer(for setters) specifically for these variables.

Above code: attr_accessor :name, :position gives you: #name, #position, #name=, and #position= methods for instance of Player class.

However, they're not going to give you validation or a customized logic for getters/setters.

For example: you might want to show a player's full name or do not wish your code to accept a 0 or negative position, in such a case you'd have to write getter and setter yourself:

class Player
  def initialize(first_name, last_name, position)
    @first_name = first_name
    @last_name = last_name
    @position = position
  end

  # validation for updating position using setter of position
  def position=(new_position)
    raise "invalid position: #{new_position}" if new_position <= 0
    @position = new_position
  end

  # customized getter for name method
  def name
    "#{@first_name} #{@last_name}"
  end
end

If you do not require customization as stated above then using attr_* method for these variables makes more sense.

Surya
  • 15,703
  • 3
  • 51
  • 74
0

Getters/setters, also known as "accessors", are accessible outside the class, instance variables are not. If you want things to be able to read or change @name from outside the class, you to define accessors for it.

Additionally accessor methods allow you to perform a certain amount of sanity checking or mutate incoming/outgoing values, and otherwise protect the internal state of your objects.

user229044
  • 232,980
  • 40
  • 330
  • 338