• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/26

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

26 Cards in this Set

  • Front
  • Back

Classes

Ruby defines the attributes and behaviors of its objects in classes. You can think of classes as basic outlines of what an object should be made of and what it should be able to do

What defines an Object?

Ruby defines the attributes and behaviors of its objects in classes. You can think of classes as basic outlines of what an object should be made of and what it should be able to do

What is a module?

As we mentioned earlier, modules are another way to achieve polymorphism in Ruby. A module is a collection of behaviors that is useable in other classes via mixins. A module is "mixed in" to a class using the include reserved word.

When defining a class, we typically focus on two things..,

When defining a class, we typically focus on two things... states and behaviors. States track attributes for individual objects. Behaviors are what objects are capable of doing.

Instance Variables are scoped at...

The object level

(from the text) For example, using our GoodDog class from earlier, we may want to create two GoodDog objects: one named "Fido" and one named "Sparky". They are both GoodDog objects, but may contain different information, such as name, weight, and height. We would use instance variables to track this information. This should tell you that instance variables are scoped at the object (or instance) level, and are how objects keep track of their states.

What are instance variables used for?

From that example, we can see that instance variables are responsible for keeping track of information about the state of an object. In the above line of code, the name of the sparky object is the string "Sparky". This state for the object is tracked in the instance variable, @name. If we created another GoodDog object, for example, with fido = GoodDog.new('Fido'), then the @name instance variable for the fido object would contain the string "Fido". Every object's state is unique, and instance variables are how we keep track.

What are the three attr* methods and what do they do?


  1. attr_reader - shorthand for getter method
  2. attr_writer - shorthand for setter method
  3. attr_accessor - shorthand for BOTH getter and setter method


Why do you need to prepend self to a setter method in a method when you already have an attr_writer? (Ex. self.name = name)

Because otherwise Ruby would think that we were instantiating a local variable. (Ex. x = 1)




To disambiguate from creating a local variable, we need to use self.name= to let Ruby know that we're calling a method.

Class methods

Class methods are methods we can call directly on the class itself, without having to instantiate any objects. We haven't implemented any class methods at this point, so let's do that now.




When defining a class method, we prepend the method name with the reserved word self., like this: def self.method

Why would we want to use a class method?

Why do we need a class method for this? This example is a little contrived, but class methods are where we put functionality that does not pertain to individual objects. Objects contain state, and if we have a method that does not need to deal with states, then we can just use a class method, like our simple example.

Class Variables

Just as instance variables capture information related to specific instances of classes (i.e., objects), we can create variables for an entire class that are appropriately named class variables.

Constants

When creating classes there may also be certain variables that you never want to change. You can do this by creating what are called constants.

to_s method

  • to_s is built into every class in Ruby
  • Left unmodified, it will call to_s on its argument

self

self can refer to different things depending on where it is used.





  • When self is prepended to a method definition, it is defining a class method. (self, inside a class but outside an instance method, is actually referring to the class itself)
  • It can refer to the calling object / instance (from within the class, when an instance method calls self, it is returning the calling object)



So we can see that self is a way of being explicit about what our program is referencing and what our intentions are as far as behavior. self changes depending on the scope it's defined in, so pay attention to see if you're inside an instance method or not.

What is the purpose of inheritance?

Inheritance is used as a way to extract common behaviors from classes that share that behavior, and move it to a superclass. This lets us keep logic in one place. Let's take a look at an example.

When should you use Inheritance vs Modules?

Now that you know the two primary ways that Ruby implements inheritance, class inheritance and mixing in modules, you may wonder when to use one vs the other. Here are a couple of things to remember when evaluating those two choices.





  • You can only subclass from one class. But you can mix in as many modules as you'd like.
  • If it's an "is-a" relationship, choose class inheritance. If it's a "has-a" relationship, choose modules. Example: a dog "is an" animal; a dog "has an" ability to swim.
  • You cannot instantiate modules (i.e., no object can be created from a module) Modules are used only for namespacing and grouping common methods together.

Does it matter the order in which you include Modules?

Yes, the order that Modules are included is important because it affect the order of the method look-up path. Ruby looks at the last module you include first and works back from there.




include Joshable


include Notable




In this example Ruby will look in the Notable Module before it looks in Joshable.

What are the 3 uses of Modules?

1. Used to mix-in common behaviors into classes


2. Namespacing: Organizing similar classes under a module


3. Modules as a container for methods, called module methods. This is useful for methods that seem out of place within your code.



Public methods

A public method is a method that is available to anyone who knows either the class name or the object's name. These methods are available for the rest of the program to use and comprise the class's interface

Private methods

A private method is a method that is doing work in the class that does not need to be available to the rest of the program.




Private methods are only accessible from other methods in the class.




You cannot use self when calling private methods because self is equivalent to calling object.private_method

Protected methods

Protected methods are best understood by the following two rules



  • from outside the class, protected methods act just like private methods. (i.e. they are not accessible)
  • from inside the class, protected methods are accessible just like public methods (i.e. you can call self on them and are not restricted to only be used by other methods in the class.)

Collaborator objects

Collaborator objects are objects from different classes that work together in some defined manner. For example, a Person object might have an instance variable called @pets that holds an array of Cat, Dog, and Fish objects. Or a Car object might hold an array of Driver and Passenger objects etc...

truthiness

Truthiness differs from true in that Ruby considers more than the true object to be "truthy". In fact, Ruby is a very liberal language and considers everything to be truthy other than false and nil.




Note that an expression that Ruby considers true is not the same as the true object. This is what "truthiness" means.




Even the integer 0 is considered truthy, which is not the case in some other languages.

short circuiting

&& and || operators exhibit a behavior called short circuiting, which means it will stop evaluating expressions once it can guarantee the return value.




&& will short circuit when it encounters the first false expression.




|| will short circuit when it encounters the first true expression.

equivalence

== is not actually an operator but is actually an instance method that derives it's comparison functions from whatever class is calling it.




Each class, custom or not, either inherits or overrides the == method to specify the value to compare




When using == we don't actually want to ask "are the two variables pointing to the same object?". We want to ask, "are the values in the two variables the same".

Integers and symbols w/ equivalence

Integers and symbols behave differently than other objects in Ruby when it comes to equivalency. If two symbols or two objects have the same value, they are also the same object. This is a performance optimization in Ruby, because you can't modify a symbol or integer.