Object-oriented programming Flashcards

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

Ruby Class

A

class is used to organize and model objects with similar attributes and methods. Strings and integers are classes but with class we can create new custom classes. Always capitalized and use CamelCase

i.e.
class NewClass
# code for this class
end

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

initialize

A

Every time Person.new is called, the initialize method of the Person class is called.

method used to generate new instances of the class.

i.e.
class Person
  def initialize
    # this code runs when a new instance is created
  end
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

instance variables

A

the @ symbol is used to signify an instance variable. Instance variables hold a value specific to each instance of that class, not to all members of the class itself

i.e.
class Student
  def initialize(name, grade)
    @name = name
    @grade = grade
  end
end

In this example, name and grade are the instance variables.

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

.new

A

a method which creates a new class instance. Arguments to the class’ inititalize method can be passed in the .new call

i.e. 
class Fighter
  def initialize(name, style, division, age)
    @name = name
    @style = style
    @division = division
    @age = age
  end
end

conor = Fighter.new(“Conor”, “mixed martial arts”, “Welterweight”, 31)

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

Class variables

A

class variables are attached to the class in which they are declared.

declared with @@ in front

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

Making a variable global from inside a class or method

A

insert $ in front of the variable

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

Inheritance

A
the process by which one class take on the attributes and methods of another. 
*a subclass can only derive from one parent class

syntax:
<

i.e.
class ApplicationError
  def display_error
    puts "Error! Error!"
  end
end
class SuperBadError < ApplicationError
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

overriding methods

A

in an inherited class, you can define the same method from the base class to override that method.

i.e.
class Creature
  def initialize(name)
    @name = name
  end

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

Add your code below!

class Dragon < Creature
  def fight #overrides fight from Creature class
    return "Breathes fire!"
  end
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

super

A

to directly access the attributes or methods of a parent class / super class in a method you have overwritten

i.e.

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

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

Add your code below!

class Dragon < Creature
  def fight 
    puts "Instead of breathing fire..."
    super()
  end
end

returns
Instead of breathing fire…
Punch to the chops!

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

public method

A

allow for an interface with the rest of the program. Can be called from outside the class

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

private method

A

can only be caled from within a class

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

attr_reader

attr_writer

A

methods used to read and write variables.

i.e.
class Person
  attr_reader :name
  attr_writer :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
13
Q

att_accessor

A

method to both read and read a variable

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

Module

A

a module contains a set of methods, constants, or classes which can be accessed. Similar to a class but cannot create instances or have subclasses. written in CamelCase

i.e.

module Circle

PI = 3.141592653589793

def Circle.area(radius)
PI * radius**2
end

def Circle.circumference(radius)
2 * PI * radius
end
end

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

constants

A

variables that always stay the same value.

Written in all caps

i.e.

PI = 3.141

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

namespace

A

To retrieve a constant from the Math module, the scope resolution operator (::), should be used.

refers to a module that contains a group of related objects.

i.e.

puts Math::PI
# => 3.141592653589793
17
Q

::

A

scope resolution operator. Used to retrieve a constant from a module

18
Q

require

A

keyword used to fetch a certain module which isn’t yet presented in the interpreter.

i.e.
require ‘date’

puts Date.today
# => 2020-04-16
19
Q

include

A

keyword to include a certain module in a class.

i,e,
include Math

20
Q

extend

A

mixes a modules method at the class level. the class itself can use the methods