ROR Questions Flashcards
Rails CRUD
Create, Read, Update, Destroy //routes.rb resources :articles
WHAT IS EAGER LOADING?
Eager loading is a great optimization strategy to reduce the number of queries that are made against the DB. clients = Client.includes(:address).limit(10) clients.each do |client| puts client.address.postcode end
What is a class? How and When to create?
A class is the blueprint from which individual objects are created. class Customer end
What is an object?
Is an instance of the class. You can create objects in Ruby by using the method new of the class. cust1 = Customer.new The initialize method is a special type of method, which will be executed when the new method of the class is called with parameters def initialize(id, name, addr) @cust_id=id @cust_name=name @cust_addr=addr end
How would you declare and use a constructor in Ruby?
Constructors are declared via the initialize method and get called when you call on a new object to be created. Order.new class Order def initialize(meal) @meal = meal end end
When to use a symbol?
Used as keys in Hashes. Used on attr_accessor.
How would you create getter and setter methods in Ruby?
Setter and getter methods in Ruby are generated with the attr_accessor method.
What is polymorphic association?
With polymorphic associations, a model can belong to more than one other model, on a single association.
What is a Proc?
short for procedures, act similar to blocks, but can be saved as variables and reused. Think of them as blocks you can call over and over again on multiple arrays. square = Proc.new do |n| n ** 2 end
What is a Block?
a block is like a proc that can’t be saved. array = [1, 2, 3, 4] array.collect! do |n| n ** 2 end
What are lambdas?
Anonymous function.
What is the difference between a class and a module?
A module cannot be subclassed or instantiated, and modules can implement mixins.
What are the three levels of method access control for classes and what do they signify? What do they imply about the method?
Public methods can be called by all objects and subclasses of the class in which they are defined in. Methods are public by default except for initialize, which is always private.
A protected method can be invoked only by objects of the defining class and its subclasses. Access is kept within the family.
Private methods cannot be accessed, or even viewed from outside the class. Only the class methods can access private members.
Explain what functional testing is:
Functional testing in Rails allows you to test the response of various actions contained in a controller.
The initialize method:
class Box
def initialize(w,h)
@width, @height = w, h
end
end