Rails Knowledge Flashcards

1
Q

Class Method vs Instance Method

A

Class methods are available on classes and Intance methods on intances.

Typically used differently - class methods used to do things on the broader scope of all instances of the class (count number of articles by a particular author) where instance methods are used to do things on a single particular instance (count number of words in this specific article)

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

What is a PORO?

A

“Plain Old Ruby Object” ; Although almost everything in Ruby is an object, ActiveRecord tends to use a lot of complex objects so the term PORO is typically used to describe a “small, simple object used to support business logic”

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

Does Ruby allow multiple inheritances?

A

No, it does not allow inheriting from more than one parent class, but it does allow module mixins with “include” and “extend”

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

Is Ruby “strongly” or “weakly” typed?

A

Strongly. An error will be thrown if you try to calculate “hello” + 3

in contrast, Javascript would calculate the same as “hello3”

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

What frameworks have you used for backgrounding jobs?

A

Sidekiq
* it uses Redis to queue jobs which is an “in-memory data store” so it is very fast
* Sidekiq adds complexity to infrastructure because of its need for redis
* we already used redis so this wasn’t an issue for us

Others out there:
* Delayed::Job - Easy to setup and use. Queues are store in DB. Could turn DB into bottleneck if same DB used for production
* Sucker Punch - Runs as a Ruby process and keeps all jobs in memory. Jobs are thus lost if the process crashes. Not recommended for critical tasks

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

How to declare a constructor on a Ruby class

A

Constructor = initialize method

initialize is called when a new instance of a class is initialized (i.e. .new is called on the class)

  • Defining this method is not required
  • Often used to provide attribute values on new instances

class Thing
attr_reader :name
def initialize(name)
@name = name
end
end
t = Thing.new(‘dog’)
puts t.name # => ‘dog

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

What logic goes into a “helper”?

A

Helper logic should support Views only

A good candidate for a helper would be date formatting logic required in several different views

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

What is ActiveRecord?

A

An ORM (Object-Relational Mapping) that maps models to db tables. It simplifies setting up an App because we no longer have to write SQL directly in order to load, save, or delete objects.

It also provides some protection against SQL injections

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

When do we use “self” in Ruby

A

When we’re calling class methods - when we’re trying to perform an action on the entire class of an object rather than just one instance

  • When defining and calling class methods
  • self refers to the current class, so its required when a class method calls another class method
  • self.class.method is required when an instance calls a class method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is “Rack”

A

Rack is an api sitting between the web server and Rails. It allows plugging in a swapping frameworks like Rails with Sinatra, or web servers like Unicorn with Puma

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

What is MVC?

A

Model View Controller

A software design pattern that Rails is built around. It splits the handling of information into 3 pieces.

The Model manages the data logic

The View displays the information

The Controller takes input and prepares data for a model or view

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

What is a block in Ruby?

A

A block is the code between two curly braces {} or b/w a “do” and “end”

You’re passing a block every time you call .each

Blocks have their own scope. Any variables defined only inside the block are not accessible outside of it, but variables defined outside of it can be modified inside the block

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

What is the difference between a Proc and a Lambda?

A

Both Procs and Lambdas are stored blocks but syntax and behavior differ slightly:

A lambda returns from itself, but a proc returns from the block it is inside of

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

What is “yield” in Ruby?

A

Yield accesses a block passed to a method. Its typically used in layout files in Rails apps

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

What is content_for for?

A

It allows rendering content in views.

Useful for defining content in one place and rendering it in many

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

What is the difference between Hash and JSON?

A

Hash is a Ruby class - a collection of key-value pairs that allows accessing values by keys

JSON is a string in a specific format for sending data

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

What is ActiveJob?

A

Allows creating background jobs and queueing them on a variety of backends like Sidekiq and Delayed::Job

Its typically used to execute code that doesn’t need to in the main web thread

Common use case is sending notification emails to users

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

What do you like about Rails?

A
  • I like the readability of it
  • I like the structure and repeatability that provides
  • I like being able to use ActiveRecord and its various tools to simplify DB manipulation/management
  • It’s fun
  • an amazing community that is super helpful and great examples and documentation is easy to find
  • Convention over configuration means you generally know where to find things when navigating a new/large codebase especially compared to frameworks that prefer configuration like Django
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What do you dislike about Rails?

A
  • Machine learning libraries are poorly developed or non existent
  • Runtime Speed and Performance. One of the most frequent arguments against RoR is its ‘slow’ runtime speed, which makes it harder to scale your RoR applications
  • Lack of Flexibility
20
Q

What is your favorite Ruby Gem?

A
  • Pry
  • Faker - for test data
  • graphql-fragment-cache
  • annotate
  • “Devise” for setting up authentication in a breeze
21
Q

What is Spring?

A

Spring is an application preloader

It keeps the application running in the background so the so that booting is not required any time you run a migration or rake task

22
Q

What is asset pipeline?

A

Its a framework that prepares Javascript and CSS for the browser

23
Q

How have you managed authentication in rails?

A

TODO

24
Q

What is the splat operator?

A

Splat is used when you don’t want to specify the number of arguments passed to a method in advance.

It is “*”

There is a single splat and a double splat in Ruby

Double receives key value pairs

25
Q

Difference between include and extend?

A

Both are mixins that allow injecting code from another module

But include allows accessing that code via class methods, while extend allows accessing that code via instance methods (This might be backwards)

26
Q

What is the difference between load and require?

A

load runs another file even if it’s already in memory

require will only run another file once, no matter how many times you require it

27
Q

What is the diff b/w a class and a module?

A

A class has attributes and methods.

A module is just a collection of methods and constants which you can mixin with another module or class

28
Q

What’s a “scope”?

A

A scope is ActiveRecord query logic that you can define inside a model and call elsewhere

Defining a scope can be useful instead of duplicating the same logic in many places in the app

29
Q

What is the diff b/w class and instance vars?

A

Instance variables denoted by the “@” are associated with an instance of a class.

Changing the value of an attribute on one instance will have no effect on the variable for another instance

Class variables denoted by “@@” are less intuitive. They are shared across all instances of the class. So, changing the variable on once instance DOES affect the variable for all instances

30
Q

What is the difference between find, find_by, and where in ActiveRecord?

A

.find takes a single argument and looks up the record where the primary key matches that argument

.find_by takes key/values and returns the first matching record

.where takes key/values and returns a collection of matching records. Or an empty collection if there are no matching records

31
Q

What is the difference between select, map, and collect?

A

.select is used to grab a subset of a collection. using .select! with a bang mutates the original collection

.map performs an action on each element of a collection and outputs an updated collection. Calling .map! with a bang mutates the original collection

.collect is an alias of .map and does the same thing

32
Q

What are the CRUD verbs and actions in Rails?

A

CRUD - Create, Return, Update, Destroy

verb action

GET index
GET new
POST create
GET show
GET edit
PATCH/PUT update
DELETE destroy

33
Q

Define a route for a create action without using “resources”

A

with resources:

resources :photos

without:

post /photos, to: photos#create, as: :create_photo

34
Q

what are the three levels of access control?

A

public, private, protected

public: Any object can call this method

protected: Only the class that defined the method and its subclasses can call the method

private: Only the object itself can call this method

35
Q

How to use singletons in Ruby?

A

A singleton is a design pattern that only allows a class to have one instance.

This is typically frowned upon in Ruby, but ruby does come with a module for it

require ‘singleton’
class Thing
include Singleton
end
puts Thing.instance
# => #<Thing:0x00007fdd492cf488>

36
Q

What is a Microservice and what is microservice architecture?

A
37
Q

What is Ruby on Rails

A

An open-source, web-application framework. Built within the Ruby programming language, it helps people create web apps quickly and efficiently by prioritizing conventions over configuration

38
Q

What do subdirectory app/controllers and app/helpers do

A

app/controllers help Rails find controller classes and essentially handles a web request from the user, the helper holds any helper classes that assist the controller, model, and view classes.

39
Q

What command can you use to create a controller for the subject?

A

rails generate controller Subject

40
Q

Name five things Rails Migration can do.

A

renaming a table, adding a column, changing a column, dropping a table, removing a column, changing the data type of a column

41
Q

What is the Rails Controller?

A

Rails Controller is the center of your application as it helps the overall user, views, and model interaction.

A controller can thus be thought of as a middleman between models and views. It makes the model data available to the view, so it can display that data to the user, and it saves or updates user data to the model.

42
Q

How can you protect Rails against Cross-Site Request Forgery?

A

To protect from any Cross-Site Request Forgery hacker attacks, you need to add ‘protect_from_forgery’ to your ApplicationController.

43
Q

What does garbage collection do in Ruby on Rails?

A

Allows the removal of the pointer values that is left behind when the execution of the program ends.

It frees the programmer from tracking the object that is being created dynamically on runtime.

44
Q

Name three of the limits of Ruby on Rails?

A

Ruby on Rails has various features that it doesn’t support which makes it unusable for some programmers. This includes linking to more than one database at a time, connecting to more than one database server at a time and foreign key in databases.

45
Q

What are some advantages of using Ruby on Rails?

A

Ruby on Rails has various advantages. For one, it makes a programmer’s work more productive. It’s also open-source and completely free. In addition, it provides programmers with the opportunity to write code that acts on actual code rather than data.

46
Q
A