Rails Knowledge Flashcards
Class Method vs Instance Method
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)
What is a PORO?
“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”
Does Ruby allow multiple inheritances?
No, it does not allow inheriting from more than one parent class, but it does allow module mixins with “include” and “extend”
Is Ruby “strongly” or “weakly” typed?
Strongly. An error will be thrown if you try to calculate “hello” + 3
in contrast, Javascript would calculate the same as “hello3”
What frameworks have you used for backgrounding jobs?
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 to declare a constructor on a Ruby class
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
What logic goes into a “helper”?
Helper logic should support Views only
A good candidate for a helper would be date formatting logic required in several different views
What is ActiveRecord?
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
When do we use “self” in Ruby
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
What is “Rack”
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
What is MVC?
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
What is a block in Ruby?
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
What is the difference between a Proc and a Lambda?
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
What is “yield” in Ruby?
Yield accesses a block passed to a method. Its typically used in layout files in Rails apps
What is content_for for?
It allows rendering content in views.
Useful for defining content in one place and rendering it in many
What is the difference between Hash and JSON?
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
What is ActiveJob?
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
What do you like about Rails?
- 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