ROR Questions 2 Flashcards
Purpose of a Module?
They provide what are called “namespaces” and, thus, prevent name clashes. Modules implement or give impetus to Ruby’s “mixin” facility.
Class method vs instance method
class Document self.do_something end end Document.do_something class Document def do_something end end Document.new.do_something
What is the exact problem with multiple inheritance?
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.
Encapsulation
Encapsulation means that the internal representation of an object is hidden from the outside. def set_name(name) @name = name end
Polymorphism
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
Ruby & Duck Typing
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
Include vs. Extend
include makes the module’s methods available to the instance of a class. extend makes these methods available to the class itself.
What Is the Difference Between a Block, a Proc, and a Lambda in Ruby?
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
Symbol vs String
Symbols are unique immutable entities as opposed to string which are mutable.
Fizz Buzz
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 would you declare and use a constructor in Ruby?
class order Def initialize(name, age) @name = name @age = age end end
Why use Symbols?
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.
attr_accessor :name
class Person attr_accessor :name end
Explain what functional testing is:
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).