Object Oriented Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q
class Person
attr_accessor :name, :age
end
p = Person.new
p.name = "Fred"
p.age = 20
puts p.instance_variables
A

[“@age, @name”]

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

enscapsulation

A
Encapsulation is the ability for an object to have certain methods and attributes available for 
use publicly (from any section of code), but for others to be visible only withinthe class itself 
or by other objects of the same class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
class Person
def anyone_can_access_this; ...; end
def this_is_private; ...; end
def this_is_also_private; ...; end
def another_public_method; ...; end
private :this_is_private, :this_is_also_private
end
A

enscapsulate methods in one line

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

plymorphism

A

writing code that can work with objects of multiple types and classes at once.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
a = Drawing.give_me_a_circle
puts a.what_am_i
a = Drawing::Circle.new
puts a.what_am_i
a = Circle.new
puts a.what_am_i
A

This is a circle
This is a circle
NameError: uninitialized constant Circle

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

constants have scope within classes (t/f)

A
True Pi = 3.14 and Pi=3 in a class are different. class::Pi = 3
Pi = 3.14
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Modules

A

provide a structure to collect Ruby classes, methods, and constants into a single,
separately named and defined unit

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

Namespaces

A

avoid name-related clashes.

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

name conflict

A

As the last file loaded, it turns out to be the latter version of random, and a random letter
should appear onscreen. Unfortunately, however, it means your other randommethod has
been “lost.”
This situation is known as a name conflict,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
module NumberStuff
def NumberStuff.random
rand(1000000)
end
end
module LetterStuff
def LetterStuff.random
(rand(26) + 65).chr
end
end
puts NumberStuff.random
puts LetterStuff.random
A

value varies from 0 - 999999 and char from A-Z

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

modules benefits

A

help solve conflicts providing namespaces that contain any number of classes, methods, and constants, and allow you to address them directly. Modules simply provide ways to organize methods, classes, and constants into separate namespaces.

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

modules are classes (t/f)

A

False. you cannot define instances of a module, nor inherit from them.

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