Object Oriented Programming Part 2 Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

How can you define whether methods are public or private and are methods public by default, yes or no?

A

private
def greeting(name)
puts “Hello, #{name}”
end

Methods are public by default yes

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

You would think that Ruby needs methods in order to access attributes, how could you edit the following code so that it’s much easier/shorter?

class Person
  def initialize(name, job)
    @name = name
    @job = job
  end

def name
@name
end

def job=(new_job)
@job = new_job
end
end

A

class Person
attr_accessor :name
attr_accessor :job

def initialize(name, job)
    @name = name
    @job = job
  end

end

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

Modules are basically like classes, but what are two main differences between the two?

A

Modules can’t create instances and can’t have subclasses.

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

If you want access a module that is not already present in the interpreter, how can you do this? Say you wanted to access the date?

A

require ‘date’

puts Date.today

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

When a module is used to mix additional behaviours and information into a class. This is very handy to customize classes without having to rewrite code. What is this called?

A

It’s called a mixin

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

Create a basic class with only an initialize method, and make it so that the balance key gets a value of 0 if it takes no argument?

A

class Account

def initialize(attrs = {})
@balance = attrs[:balance] || 0
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly