Object Oriented Programming Part 1 (Classes) Flashcards
Create a basic class
class Person
end
What are the different kind of variables when it comes to classes in Ruby and explain what each one means?
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
What is the correct syntax for each kind of variable?
Global: $age
Instance: @age
Class: @@age
Local: age
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
class MyClass $my_variable = "Hello!" end
puts $my_variable
or
my_variable = "Hello!" class MyClass
end
puts my_variable
How would you create a class that inheritance from the class Animal?
class Animal def initialize(name, age) @name = name @age = age end
class Animal def initialize(name, age) @name = name @age = age end end
class Fish < Animal
end
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
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
If you want to write a class or method with nothing inside of it, whats a very quick/short way of writing this?
class Monkey; end