Ruby on Rails - Avoid Instance Variables, Use Getters and Setters - ruby on rails tutorial - rails guides - rails tutorial - ruby rails



Instance Variables:

  • An instance variable has a name beginning with @, and its scope is confined to whatever object self refers to.
  • Two different objects, even if they belong to the same class, are allowed to have different values for their instance variables.

Getters and Setters:

  • A getter is a method that gets the value of a specific property.
  • A setter is a method that sets the value of a specific property.
  • You can define getters and setters on any predefined core object or user-defined object that supports the addition of new properties.
  • In Ruby, you can set and access instance variables like this:
@bar = "bar"
@bar #=> "bar"
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
  • But you shouldn't. You should always use getter and setter methods like so:
self.bar = "bar"
bar #=> "bar"
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
  • This is because instance variables never raise errors so this could happen to you:
@bar = "bar"
@barr #=> nil
# or...
@barr = "bar"
@bar #=> nil
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
  • With getters and setters, you won't waste time tracking down bugs caused by spelling errors like the one above:
self.barr = "bar" #=> raises NoMethodError since `barr=`is not a method
barr #=> raises NoMethodError because `barr` is not a method
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
  • You don't even need to write the getters and setters yourself, you can just use attr_accessor (or attr_writer or attr_reader if you only want one or the other).
  • If you don't want to expose the getters or setters to the outside world then you can always make them private:
class Foo
  def get_bar
    # log something...
    bar
  end

  def set_bar(value)
    # log something...
    self.bar = value
  end

  private

  attr_accessor :bar
end

foo = Foo.new

# These raise errors since we're calling private methods
foo.bar = "bar" #=> error!
foo.bar #=> error!

# But our public interface can still use these methods
foo.set_bar("foo")
foo.get_bar #=> "foo"
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team


This ruby on rails tutorial page provides you the following key areas such as ruby , rail , ruby on rails , rail forum , ruby on rails tutorial , ruby tutorial , rails guides , rails tutorial , learn ruby , rails form_for , ruby rails , ruby class , what is ruby on rails , rails installer , ruby online , learn ruby on rails , ruby on rails jobs , rails find_by , install rails , easyrail , rubyonrails , link_to rails , ruby on rails developer , learn ruby the hard way , railscasts , ruby on rails examples , ruby on rails vs php , rails 4 , rails activerecord , rails generate , ruby and rails , ruby on rails download , install ruby on rails , ruby net http , what is rails , ruby app , ruby vs ruby on rails , ruby on rails windows , rails for zombies , ruby on rails book , ruby on rails development , ruby on rails ide , ruby on rails tutorial pdf

Related Searches to Avoid Instance Variables, Use Getters and Setters