ROR Questions 2 Flashcards

1
Q

Purpose of a Module?

A

They provide what are called “namespaces” and, thus, prevent name clashes. Modules implement or give impetus to Ruby’s “mixin” facility.

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

Class method vs instance method

A

class Document self.do_something end end Document.do_something class Document def do_something end end Document.new.do_something

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

What is the exact problem with multiple inheritance?

A

The most obvious problem is with function overriding. Let’s say have two classes A and B, both of which define a method “doSomething”. Now you define a third class C, which inherits from both A and B, but you don’t override the “doSomething” method.

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

Encapsulation

A

Encapsulation means that the internal representation of an object is hidden from the outside. def set_name(name) @name = name end

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

Polymorphism

A

is the provision of a single interface to entities of different types. def print p ‘Print from XmlDocument’ end def print p ‘Print from HtmlDocument’ end

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

Ruby & Duck Typing

A

Rather than having to check that an object is of a specific type, all we are concerned about is that the object can answer to a method call in a specific context. class Printer def initialize(document) @document = document end def print @document.print end end

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

Include vs. Extend

A

include makes the module’s methods available to the instance of a class. extend makes these methods available to the class itself.

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

What Is the Difference Between a Block, a Proc, and a Lambda in Ruby?

A

Procs are objects, blocks are not At most one block can appear in an argument list Lambdas check the number of arguments, while procs do not Lambdas and procs treat the ‘return’ keyword differently. ‘return’ inside of a lambda triggers the code right outside of the lambda code

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

Symbol vs String

A

Symbols are unique immutable entities as opposed to string which are mutable.

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

Fizz Buzz

A

def fizzbuzz?(number) return ‘Fizzbuzz’ if number % 15 == 0 return ‘Buzz’ if number % 5 == 0 return ‘Fizz’ if number % 3 == 0 number end (1..100).each { |i| p fizzbuzz?(i) }

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

How would you declare and use a constructor in Ruby?

A

class order Def initialize(name, age) @name = name @age = age end end

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

Why use Symbols?

A

Symbols should be used whenever referring to a name (identifier or keyword), even if that name doesn’t exist in actual code yet. Naming keyword options in a method argument list Naming enumerated values. Naming options in an option hash table.

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

attr_accessor :name

A

class Person attr_accessor :name end

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

Explain what functional testing is:

A

Functional testing in Rails allows you to test the response of various actions contained in a controller. will tell your testing library to expect a certain response based on a control request passed in (either a get, post, patch, put, head, delete request).

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