Rails Basics Flashcards
What is Rails
open source web application framework written in Ruby.
Active Record Pattern
a database table is represented as a class; each row of the tables is created/updated by an object instance.
Convention over Configuration
A software design that decreases the number of decisions to be made by developers
E.g. Rails automatically render views with names that correspond to valid routes.
What is a Framework?
A framework is a program or code library that writes most of your application for the developer.
What is MVC?
Model: Application data.
View: Presentation layer
Controller: Processes and responds to events
How does MVC work?
Request first comes to the controller, controller finds the appropriate view and interacts with model, model interacts with your database and send the response to controller then controller based on the response give the output parameter to view
Explain RESTful Architecture
REST = REpresentational State Transfer
It uses HTTP methods GET, PUT, POST, or DELETE
RESTful interface means clean URLs, less code.
Why Rails?
DRY Principal( Don’t Repeat Yourself)
Convention over Configuration
Gems
What is the ORM of rails?
ORM stands for Object-Relationship-Model, where classes are mapped to tables in the database, and objects are directly mapped to the rows in the table
What Associations Relationships does a Model have?
one-to-one: exists when one item has exactly one of another item.
one-to-many: exists when a single object can be a member of many other objects.
many-to-many: exists when the first object is related to one or more of a second object, and the second object is related to one or many of the first object.
belongs_to has_one has_many has_many :through has_one :through has_and_belongs_to_many
What is the Ruby naming convention for methods?
Lowercase names that may be followed by digits, underscores, letters, e.g. paint or close_the_door
What are Instance and Class variables?
Instance variables(@): the data varies with each instance of the object. @i = 1
Class variables(@@): “static variables”, shared across all instances of a class. @@i = 1
What is the difference between empty , nil and blank?
Nil: used on any object and is true if the object is nil
Empty: can be used on strings, arrays and hashes and returns true if:
String length == 0;
Array length == 0;
Hash length == 0;
.blank? evaluates true on strings which are non-empty but contain only whitespace.
What is a destructive method?
It is a method that alters the attribute of an object.
Example:
myabcs = [‘a’, ‘b’, ‘c’] #myabcs is of type Array
myabcs.reverse! #this method altered the object
p myabcs #outputs
#[[“c”,”b”,”a”]
What are the variable scopes?
Local variables are local to the code construct in which they are declared. eg: method, loop.
must begin with either an underscore or a lower case letter
i = 5
Global variables accessible from anywhere in the Ruby program.
$speed = 10
Class variable is shared amongst all instances of a class. Only one variable value exists for all objects. If one object instance changes the value of the variable, that new value will change for all other object instances.
@@i = 5
Instance variables are local to specific instances of an object.
@i = 5
Ruby constants are values which, once assigned a value, should not be changed.
GRAVITATIONAL_CONST = 6.67
Name a few object oriented concepts in Ruby.
Classes, Objects, Inheritance, Singleton methods, polymorphism(accomplished by overriding and overloading) are some object oriented concepts supported by Ruby
Provide an example if how an iterator is used?
times Iterate through the block 3 times, n starts at 0 and ends at 2 and is incremented by 1 each time. n is the block variable. 3.times do |n| puts n end
upto Iterate through the block 3 times, starting at 1 and ending at 3, each time incrementing by 1. n is the block variable. 1.upto(3) do |n| puts n en
downto Iterate through the block 3 times, starting at 3 and ending at 1, each time decrementing by 1. n is the block variable. 3.downto(1) do |n| puts n end
When to use Load vs Require in Ruby?
Require loads the file only once, while load loads it every time it is called
Use load rather than require if you need to force something to reload (e.g. a web server or a test server)
Load takes in a file’s full name path such as:
load “/path/to/file.rb”
Argument vs Parameter
The parameter is what is declared/ defined in a method or a class An argument is the value that is passed when calling the function or instantiating the class. Example: class Person attr_accessor :name, :age def initialize(name = '', age = 0) self.name = name self.age = age
RESTfull Resources
GET /photos photos#index GET /photos/new photos#new POST /photos photos#create GET /photos/:id photos#show GET /photos/:id/edit photos#edit PATCH/PUT /photos/:id DELETE /photos/:id