Ruby Flashcards
What is a class?
A blueprint/template for attributes and behaviour of objects
- attributes and instance values
- methods
What is an object?
An individual instance of a class
What are the three levels of method access control for classes and modules? What do they imply about the method?
Public, Private, Protected
Pu: default, accessible by all instance objects
Pri: can only be called within the scope of an object. E.g. By other methods of the class
No explicit receiver. I.e. Cant be called on object or self
Pro: must be within the scope of an object, can accept an explicit receiver like self, can be called on objects of the same class
NB: explicit receivers
What are the three ways to invoke a method in Ruby?
x = Object.new
puts x.some_method
x. send(:some_method)
x. method(:some_method).call
Explain this idiom: a ||= b
Conditional assignment
Shorthand for a = a || b
Take notenof boolean case where a = false:
If a = nil or false, it will be assigned b’s value
What does self mean?
Gives access to the current object. i.e. The object that is receiving the message
What is a block?
A nameless method that can be passed to another method as a parameter
syntax: do…end, {}
What is a Proc?
a block of code that can be bound to a set of local variables
What is a Lambda?
an anonymous function (of class Proc)
lambda { … }
lambda do … end
-> { … }
argument for higher order functions
What is the difference between a proc and a lambda?
…
What is unit testing? What is a primary technique when doing unit testing?
…
What is a ruby module?
It is a container for methods and constants
Allows u to mixin and share methods and constants across many classes
“include” is used to mix in a module
What is the difference between include, load, require and extend?
Include: methods available to instances (adding instance methods)
Extend: method is available to class itself, not instances of the class
If a module method is prefixed with self it is not accessible by neither a class nor its instances
Does ruby support single inheritance or multiple inheritance or both?
…
What is the difference between:
“and” and “&&”
And
“or” and “||”
…