Ruby Flashcards

1
Q

What is encapsulation and describe the forms of encapsulation.

A

The ability for an object to have certain methods and attributes available for use publicly from any section of code. Other methods and attributes are visible only within the class itself or by other objects of the same class.

Forms of encapsulation
1. public methods
Can be called by anyone—there is no access control. Methods are public by default (except for initialize, which is always private).

  1. private methods
    can only be used within a class. It’s for internal usage. Think of private methods as internal helper methods. The only way to have external access to a private method is to call it within a public method. Also, private methods can not be called with an explicit receiver, the receiver is always implicitly self. Because you cannot specify an object when using them, private methods can be called only in the defining class.
class Person
  def speak
    puts "Hey, Tj!"
  end
  def whisper_louder
    whisper
  end 
# private methods are for internal usage within the defining class
  private 
  def whisper
    puts "His name's not really 'Tj'." 
  end 
end
you = Person.new 
you.speak # "Hey, Tj!"
a_hater = Person.new
a_hater.speak # "Hey, Tj!"
a_hater.whisper # NoMethodError
a_hater.whisper_louder # "His name's not really 'Tj'."

We can only access the #whisper method externally by defining a public method, #whisper_louder, that calls it.

3. protected methods 
Very similar to private methods, except Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.
So protected methods can be called with an explicit receiver, so long as the caller is of the same class.

For more info:

https: //github.com/appacademy/curriculum/blob/39e94694a11278c9bb02f3f00a89762ec789e6ce/ruby/readings/privacy.md
https: //github.com/appacademy/curriculum/blob/master/ruby/readings/hiding.md

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Describe polymorphism.

A

The concept of writing code that can work with objects of multiple types and classes at once. For example, “+” method works for adding numbers, joining strings, and adding arrays together.

Some Ruby built in standard classes (Array, Hash, String) have polymorphic methods of their own. For example, you can call “to_s” method on many built in classes to return contents of object as a string.

For more info:
pg 135 “Beginning Ruby”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Name a few Ruby best practices.

A
  1. When building a Ruby on Rails app and we want the bulk of our non-response related logic in our Models, as opposed to in our Controllers, following “Fat model skinny controller” best practice.
  2. Shy code: The more you expose to users of the class (whether people or other code), the more they will rely on those details.
    2a. The point of object orientation is to present a simple interface, abstracting away the details inside the method implementations. Code that is too permissive breaks those abstractions, leaking internal concerns to the outside world. For instance, if we let a user call start_engines, now they need to remember to later call stop_engines.
  3. A good guiding principle of OO design is: minimize the interfaces between your classes; expose the least possible amount of state and behavior, and have a good reason for every piece of information that you expose.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the primary purpose of access control (private, public, protected)?

A

Note that these access modifiers are not for security. In fact, they’re super easy to subvert.

Instead of security, you should be using access controls to describe to other programmers reading your code:

What methods are the “interface” that they’ll want to use, and what are underlying details they may wish to ignore.
What methods are “supported” and public, and which ones are liable to change. Private methods, because they usually are focused on internal details, often are removed or changed as the code grows. There is typically a greater effort to continue to support and not break the existing public interface.

For more info:
https://github.com/appacademy/curriculum/blob/39e94694a11278c9bb02f3f00a89762ec789e6ce/ruby/readings/privacy.md

How well did you know this?
1
Not at all
2
3
4
5
Perfectly