Ruby Flashcards

1
Q

What is a Proc?

A

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.” }

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

What does self mean?

A

Self refers to the current object. Similar to this in js.

in an instance of the calss (object) self will return that object.

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

a ||= b

A

if a is already defined it shall remain. however if a has a falsey value, it will then point to b.

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

What are the 3 levels of method access control in classes and modules?

A

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

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

What are the 3 ways to invoke methods in ruby?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a class?

A

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!

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

What is an object?

A

An instance of a class

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

What is a module? (and why?)

A

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)

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