Object Oriented Programming in Ruby Flashcards
Define a Class: Dog,
let it have a Function: bark.
It can say anything you want.
class Dog
def bark
“woof”
end
end
Given a class named Dog, create a child class that inherrits the bark method.
bonus, can you override the bark method?
Class ChildDog < Dog
end
Class ChildDog < Dog
def bark
“miauw”
end
end

Make a class named MathFunctions. Then outside the class, define a classmethod for Mathfunctions called Tripple. It should take variable and tripple's it.
Class MathFunctions
en
def MathFunctions.tripple(var)
var *3
end

Write an initializer for Person(name),
Write the getter and setter Method,
don’t use any of the attr_<***>.
Class Person
def initialize(name)
@name = name
end
def name
@name
end
def name=(new_name)
@name = new_name
end

How would you build a contructor for a Person Class
He/She needs a name and age!
def initialize(name, age)
@name = name
@age = age
end

Make a onliner for attribute setter
and getter for the variable: name
attr_accessor :name
For the initialize constructor:
Let age be a Class Method.
given initialize only takes name and age.
check if age is not over 120.
def initialize(name, ageVar)
@name = name
self.age = ageVar
end
def age=(new_age)
@age = new_age unlesss new_age > 120
end

There basicly 3 ways to add a Class Method,
witch ones?
Where should they be declared?
inside the class:
class << self
def method_in_class_in_class
end
end
def self.method_in_class
end
outside the class:
def TheClass.theMethod(variables)
end

Define a class variable named: counter
@@counter
How would a method look like
inside the scope of a constructor?
example:
def initialize(age)
self.age = age
end
def age=(new_age)
@age ||= 5
@age = new_age unless new_age > 120
end

Scope: Constants
How is a Constant defined,
whats the diffrent with normal variables?
MyConstant = “This string is a constant”
- Inner scope can see Constants defined on the outside, and even change them.
- The value of the outher scope doesn’t change with changing the Constant on the inner scope.
# define v1 = "outside" outside of a class define v1="inside" inside of a class MyClass,
inside of a class my_method.
v1 = “outside”
class MyClass
def my_method
v1 = “inside”
end
end

Define a module named: Test,
in with a constant pi lives.
it should hold the value of pi (3.14 )
The module should have a inner class,
and a method putting out pi.
How would you call it ?
How would you include it?
module Test
PI = 3.14
class Test2
def what_is_pi
puts PI
end
end
end
Test::Test2.new.what_is_pie # => 3.14

Would a constant defined inside module,
be readeble for a class outside that module? or a class inside in that module?
it would if the module would be included, like
require_relative ‘module’ .
Yes, classes can handle Constants in Modules defined on the outher scope.

Implement a module namend “foobar”.
include ‘foobar’
What are the reasons for using a module?
Modules are best in use for grouping
Methods and Functions for two main reasons:
A: name spacing
B: mixins

What does Mixin mean?
Sharing functionality, functions, methods and code
with other modules, classes, and objects.
Like ‘enumerable’

What is namespacing?
a way to let objects with the same name
live through seperating them.

Can you implement Enumerable inside the
Class Team ?
class Team
include Enumerable # good functionality
… …. …
def each # => needed to initiate Enumerability
@player.each { | player | yield player }
end

# Define a module Sports that has a class of Match. and a method that holds @number
getter and setter.
make a new1 match object.
give it a score of 35
module Sports
class Match
def match_count
attr_accessor :number
end
end
end
new1 = Sports::Match.new
new1.score = 35 ; #=> puts new1.score == 35

You have a player.rb file.
Inside it, include game.rb, and match.rb
class Player
require_relative ‘game’
require_relative ‘match’
end

You have a module Math with a class Calculator. Create a new instance my\_math from it.
module Math
class Calculator
attr_accessor :results
end
end
my_math = Math::Pi.new
my_math.results = “new result”

Scope : block
Describe what a block is, and what it’s scope is.
A block is a closure.
Block inherrits from the outher scope, unlike a method.
Blocks remember the context in witch they where created, and uses that context whenever it is called.

Enumerable,
name 4 methods from the Enumberable.
Can it be used on other classes?
each select reject map
yes,

def each
@enumberable.each { | enumberable | yield |enumberable
end
True or False
Private methods can be invoked with an explicit receiver.
Are there any exceptions?
false,
except Setting an attribute.

Fill in the blanks
Methods and Classes ____ a scope.
Constants ____ scope.
Blocks ___ scope.
___ can be overriden
Methods and classes start a new scope
Constants maintain their scope.
Blocks inherit outher scope
blocks could be overridden.

Given var my_var = 5, how can you override
it whitin the block?
Add it ad a argument and reasign it.

ex: arr.each do |var, my_var|
my_var = 10
Whitin a class you can set methods private,
What ways can you do that?
Placing them below the private statement.
everything below ‘ private’ is a private method.
or enlisting them as a key to the private hash.
private :method, :method, method

Methods and Functions can be -or not -
reacable, what 3 states can they be?
Private Public Protected
Public methods can be called by anybody,
what can call protect methods?
Protected methods can be called by objects
of the defining class or its subclasses.

Make rspec use a new calc variable,
an instance of the calculator class.
test for substraction, or addition
require ‘test/unit’
require_relative ‘calculator’
class ClaculatorTest < Test::Unit::TestCase
def setup
@calc = Calculator.new(‘test’)
end
def addition
assert_equal 4, @calc.add(2.2)
end

Unittest, a zero devision error for
calc.devide(1,0)
def test_divide_by_zero
assert_raise ZeroDivisionError do
@calc.divide(1,0)
end

The minitest Setup() & Teardown() equvelent in Rspec
before()
after()

Rspec
Rspec

Describe Rspec it should be even
it “these numbers should be “ do
expect(@mathod.afunction(arg,arg)).to be _even
expect(@mathod.afunction(arg,arg)).not_to be_odd
end
