Object Oriented Programming in Ruby Flashcards

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

Define a Class: Dog,

let it have a Function: bark.
It can say anything you want.

A

class Dog

def bark

“woof”

end

end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
Given a class named Dog,
create a child class that inherrits the bark method.

bonus, can you override the bark method?

A

Class ChildDog < Dog
end

Class ChildDog < Dog

def bark

“miauw”

end

end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
Make a class named MathFunctions.
Then outside the class,
define a classmethod for Mathfunctions called Tripple.
It should take variable and tripple's it.
A

Class MathFunctions

en

def MathFunctions.tripple(var)

var *3

end

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

Write an initializer for Person(name),
Write the getter and setter Method,
don’t use any of the attr_<***>.

A

Class Person

def initialize(name)

@name = name

end

def name

@name

end

def name=(new_name)

@name = new_name

end

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

How would you build a contructor for a Person Class

He/She needs a name and age!

A

def initialize(name, age)

@name = name

@age = age

end

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

Make a onliner for attribute setter

and getter for the variable: name

A

attr_accessor :name

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

For the initialize constructor:
Let age be a Class Method.

given initialize only takes name and age.

check if age is not over 120.

A

def initialize(name, ageVar)

@name = name

self.age = ageVar

end

def age=(new_age)

@age = new_age unlesss new_age > 120

end

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

There basicly 3 ways to add a Class Method,

witch ones?
Where should they be declared?

A

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

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

Define a class variable named: counter

A

@@counter

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

How would a method look like

inside the scope of a constructor?

A

example:

def initialize(age)

self.age = age

end

def age=(new_age)

@age ||= 5

@age = new_age unless new_age > 120

end

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

Scope: Constants
How is a Constant defined,

whats the diffrent with normal variables?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
# define v1 = "outside" outside of a class
define v1="inside" inside of a class MyClass,

inside of a class my_method.

A

v1 = “outside”

class MyClass

def my_method

v1 = “inside”

end

end

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

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?

A

module Test

PI = 3.14

class Test2

def what_is_pi

puts PI

end

end

end

Test::Test2.new.what_is_pie # => 3.14

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

Would a constant defined inside module,

be readeble for a class outside that module?
or a class inside in that module?
A

it would if the module would be included, like

require_relative ‘module’ .

Yes, classes can handle Constants in Modules defined on the outher scope.

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

Implement a module namend “foobar”.

A

include ‘foobar’

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

What are the reasons for using a module?

A

Modules are best in use for grouping
Methods and Functions for two main reasons:

A: name spacing
B: mixins

17
Q

What does Mixin mean?

A

Sharing functionality, functions, methods and code

with other modules, classes, and objects.

Like ‘enumerable’

18
Q

What is namespacing?

A

a way to let objects with the same name

live through seperating them.

19
Q

Can you implement Enumerable inside the
Class Team ?

A

class Team

include Enumerable # good functionality

… …. …

def each # => needed to initiate Enumerability

@player.each { | player | yield player }

end

20
Q
# 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

A

module Sports

class Match

def match_count

attr_accessor :number

end

end

end

new1 = Sports::Match.new

new1.score = 35 ; #=> puts new1.score == 35

21
Q

You have a player.rb file.
Inside it, include game.rb, and match.rb

A

class Player

require_relative ‘game’

require_relative ‘match’

end

22
Q
You have a module Math with a class Calculator.
Create a new instance my\_math from it.
A

module Math

class Calculator

attr_accessor :results

end

end

my_math = Math::Pi.new

my_math.results = “new result”

23
Q

Scope : block
Describe what a block is, and what it’s scope is.

A

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.

24
Q

Enumerable,
name 4 methods from the Enumberable.

Can it be used on other classes?

A

each select reject map

yes,

def each

@enumberable.each { | enumberable | yield |enumberable

end

25
Q

True or False
Private methods can be invoked with an explicit receiver.

Are there any exceptions?

A

false,

except Setting an attribute.

26
Q

Fill in the blanks

Methods and Classes ____ a scope.
Constants ____ scope.
Blocks ___ scope.
___ can be overriden

A

Methods and classes start a new scope

Constants maintain their scope.

Blocks inherit outher scope

blocks could be overridden.

27
Q

Given var my_var = 5, how can you override

it whitin the block?

A

Add it ad a argument and reasign it.

ex: arr.each do |var, my_var|
my_var = 10

28
Q

Whitin a class you can set methods private,

What ways can you do that?

A

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

29
Q

Methods and Functions can be -or not -

reacable, what 3 states can they be?

A

Private Public Protected

30
Q

Public methods can be called by anybody,

what can call protect methods?

A

Protected methods can be called by objects

of the defining class or its subclasses.

31
Q

Make rspec use a new calc variable,

an instance of the calculator class.

test for substraction, or addition

A

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

32
Q

Unittest, a zero devision error for

calc.devide(1,0)

A

def test_divide_by_zero

assert_raise ZeroDivisionError do

@calc.divide(1,0)

end

33
Q

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

A

before()

after()

34
Q

Rspec

A

Rspec

35
Q

Describe Rspec it should be even

A

it “these numbers should be “ do

expect(@mathod.afunction(arg,arg)).to be _even

expect(@mathod.afunction(arg,arg)).not_to be_odd

end