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 “||”
…
What type of variables are there in ruby?
…
What is the difference between method overloading and method overwriting?
…
What is a Destructive Method?
…
What is the difference between a string and a symbol?
…
What are Enumerator and enumberable?
…
What is the scope of a local variable?
…
How does Garbage Collection work in Ruby?
…
Whats the difference between puts and print?
…
What are float, dig and max?
…
What is metaprogramming?
…
What is the purpose of adding “!” Or “?” At the end of a method name?
…
What is the difference between a class and a module?
Modules cant be instantiated… more like containers for constants and methods. Can be mixed in to classes
Classes can be instantiated. When module is included a proxy superclass is generated to provide access to all methods in module
What is meant by “everything is an object” in Ruby?
Ruby doesnt have primitives, and data types are inherited from super classes
Methods, operators and blocks are not objects though
What are the main aspects of OOP?
Encapsulation
Inheritance
How does ruby “know” where to get a method that is called on an object?
There is a lookup path that looks up the method in included models and superclasses…
[class] [modules] Object Kernel BasicObject
What does the splat operator * do in method definition and in method calls?
Definition: optional arguments passed as an array
E.g def something(x, y, *options)
Call: pass an array into method and each element will be passed as if it was passed directly into method
What options are available for method arguments?
Default values Splat operators for last arg Hash arguments Blocks Keyword arguments Ampersand operator for last arg
What is the ampersand operator used for in method definition? In method call?
Def: Shows that method expects a block to be passed in
A proc will be created and assigned to that parameter
Call: a proc object preceded by an ampersand can be passed into a method and the contained block can replace the method using “yield” method