Ruby Flashcards

1
Q

What is a class?

A

A blueprint/template for attributes and behaviour of objects

  • attributes and instance values
  • methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an object?

A

An individual instance of a class

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

What are the three levels of method access control for classes and modules? What do they imply about the method?

A

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

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

What are the three ways to invoke a method in Ruby?

A

x = Object.new
puts x.some_method

x. send(:some_method)
x. method(:some_method).call

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

Explain this idiom: a ||= b

A

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

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

What does self mean?

A

Gives access to the current object. i.e. The object that is receiving the message

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

What is a block?

A

A nameless method that can be passed to another method as a parameter

syntax: do…end, {}

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

What is a Proc?

A

a block of code that can be bound to a set of local variables

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

What is a Lambda?

A

an anonymous function (of class Proc)

lambda { … }
lambda do … end
-> { … }

argument for higher order functions

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

What is the difference between a proc and a lambda?

A

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

What is unit testing? What is a primary technique when doing unit testing?

A

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

What is a ruby module?

A

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

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

What is the difference between include, load, require and extend?

A

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

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

Does ruby support single inheritance or multiple inheritance or both?

A

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

What is the difference between:

“and” and “&&”

And

“or” and “||”

A

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

What type of variables are there in ruby?

A

17
Q

What is the difference between method overloading and method overwriting?

A

18
Q

What is a Destructive Method?

A

19
Q

What is the difference between a string and a symbol?

A

20
Q

What are Enumerator and enumberable?

A

21
Q

What is the scope of a local variable?

A

22
Q

How does Garbage Collection work in Ruby?

A

23
Q

Whats the difference between puts and print?

A

24
Q

What are float, dig and max?

A

25
Q

What is metaprogramming?

A

26
Q

What is the purpose of adding “!” Or “?” At the end of a method name?

A

27
Q

What is the difference between a class and a module?

A

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

28
Q

What is meant by “everything is an object” in Ruby?

A

Ruby doesnt have primitives, and data types are inherited from super classes

Methods, operators and blocks are not objects though

29
Q

What are the main aspects of OOP?

A

Encapsulation

Inheritance

30
Q

How does ruby “know” where to get a method that is called on an object?

A

There is a lookup path that looks up the method in included models and superclasses…

[class]
[modules]
Object
Kernel
BasicObject
31
Q

What does the splat operator * do in method definition and in method calls?

A

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

32
Q

What options are available for method arguments?

A
Default values
Splat operators for last arg
Hash arguments
Blocks
Keyword arguments 
Ampersand operator for last arg
33
Q

What is the ampersand operator used for in method definition? In method call?

A

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