Code Monkey home page Code Monkey logo

phase-3-ruby-oo-basics-setter-and-getter-methods's Introduction

Setter and Getter Methods

Learning Goals

  • Learn more about setter and getter methods
  • Build and use setter and getter methods

Introduction

So far, we've learned how to build classes and give them instance methods. We also learned how to create instance variables and use setter and getter methods to set and retrieve their values, respectively. In this lesson, we'll learn more about these methods and why they're important.

Setters and Getters

Setter and getter methods are integral to object-oriented programming in Ruby. They are what enable us to assign attributes to our objects (e.g., name or breed) and retrieve the value of those attributes.

To review, if we have a Person class, our setter and getter for the name attribute will look like this:

class Person

  # setter method
  def name=(name)
    @name = name
  end

  # getter method
  def name
    @name
  end

end

Note that the setter method is defined with an = (equals sign) appended to the name of the method. The = is then followed by the parameter, name, in parentheses. The = in the method definition line is part of the method's name; it is simply a character that identifies this method as a setter method. The actual assignment โ€” the use of the operator = to set a value โ€” happens inside the method.

What this means is that, if we were to create a Person instance and then use the usual Ruby syntax to call the setter method and pass it an argument, our code would look like this:

kanye = Person.new
kanye.name=("Kanye")

Note that this is exactly the same as calling any method that takes an argument on any object:

any_object.any_method(arg)

The only difference is the = that is part of our method's name, identifying it as a setter method.

We don't have to use this syntax to call our setter method, however. Instead, Ruby provides us a bit of syntactic sugar for setter methods that enables us to do this:

kanye = Person.new
kanye.name = "Kanye"

This syntax is easier to read and more intuitive and is therefore preferred.

Finally, to ask our kanye object to return the value of its name attribute, we would do this:

kanye.name #=> "Kanye"

Here we are calling the getter method, name, that belongs to every instance of our Person class.

Instructions

Fork and clone the lab and run learn test. To get the tests passing, you will need to complete the following tasks:

Dog and lib/dog.rb

  1. Define a name getter and a name= setter method for the instance variable @name.
  2. Define a breed getter and a breed= setter method for the instance variable @breed.

Dog Breeds

Person and lib/person.rb

  1. Define a name getter and a name= setter method for the instance variable @name.
  2. Define a job getter and a job= setter method for the instance variable @job.

phase-3-ruby-oo-basics-setter-and-getter-methods's People

Contributors

aviflombaum avatar annjohn avatar lizbur10 avatar ihollander avatar maxwellbenton avatar drakeltheryuujin avatar ralst0n avatar sammarcus avatar timothylevi avatar victhevenot avatar emill3000 avatar jonbf avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.