Object Oriented Programming Part 1 (Classes) Flashcards

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

Create a basic class

A

class Person

end

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

What are the different kind of variables when it comes to classes in Ruby and explain what each one means?

A
global variables: available everywhere
local variables: available only within certain methods
class variables: available to a certain a class
instance variables: available to certain instances of a class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the correct syntax for each kind of variable?

A

Global: $age
Instance: @age
Class: @@age
Local: age

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

Fix the following class so that my_variable can be printed out to the console, write out both ways please

class MyClass
  my_variable = "Hello!"
end

puts my_variable

A
class MyClass
  $my_variable = "Hello!"
end

puts $my_variable

or

my_variable = "Hello!"
class MyClass

end

puts my_variable

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

How would you create a class that inheritance from the class Animal?

class Animal
def initialize(name, age)
@name = name
@age = age
end
A
class Animal
def initialize(name, age)
@name = name
@age = age
end
end

class Fish < Animal

end

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

Create a class called Dragon that overrides the fight method in the Creature class.

class Creature
  def initialize(name)
    @name = name
  end

def fight
return “Punch to the chops!”
end
end

A
class Creature
  def initialize(name)
    @name = name
  end

def fight
return “Punch to the chops!”
end
end

class Dragon
def fight
return "Breathes fire!"
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

If you want to write a class or method with nothing inside of it, whats a very quick/short way of writing this?

A

class Monkey; end

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