Ruby Flashcards
What is a Proc?
Procs are basically anonymous functions. Not to be confused with a block. They can be placed inside a variable and used that way.
ex:
anonymous = Proc.new { puts “I’m a Proc for sure.” }
What does self mean?
Self refers to the current object. Similar to this in js.
in an instance of the calss (object) self will return that object.
a ||= b
if a is already defined it shall remain. however if a has a falsey value, it will then point to b.
What are the 3 levels of method access control in classes and modules?
Public methods enforce no access control – they can be called in any scope.
Protected methods are only accessible to other objects of the same class.
Private methods are only accessible within the context of the current object
What are the 3 ways to invoke methods in ruby?
dot operator (or period operator), the Object#send method, or method(:foo).call
ex:
object = Object.new
puts object.object_id
#=> 282660
puts object.send(:object_id) #=> 282660
puts object.method(:object_id).call # (Kudos to Ezra) #=> 282660
What is a class?
A class is a template for objects.
Further:
Classes can hold data and have methods that interact with that data and are used to instantiate objects!
What is an object?
An instance of a class
What is a module? (and why?)
Modules serve as a mechanism for namespaces?
Modules allow you to create code that you can share across multiple needs. “Modular” Can be used to share methods aside from classic class inheritance. (mix-ins)