Ruby on Rails Flashcards
What is a test double?
A test double is a simplified object that allows us to define “fake” methods and their return values.
What are stubs compared to mocks?
A method stub is returns a pre-determined value. A mock is a stub with a built-in expectation to be satisfied during the test.
What are spies?
Spies are test double objects that record information about how functions are called, such as the arguments passed to them and the values they return
What are 3 ways to import a module into a class?
- via include keyword:
include inserts the module into the ancestor chain of the class importing the module - via extend keyword:
extend imports modules as class methods. - via prepend keyword:
prepend inserts the module at the bottom of the chain even before the class
What is Ruby on Rails and how have you used it?
Ruby on Rails is a robust web application framework built on the Ruby programming language. It provides a structured and elegant way to develop web applications by emphasizing convention over configuration. I’ve utilized Ruby on Rails extensively in my previous role at Shopify as a Backend Engineer. For instance, I used Rails to develop essential components of Shopify Payments, including integrating new payment methods such as China UnionPay and migrations to update rates for merchants. Rails’ conventions and modular structure contributed to building scalable, maintainable, and secure foundation for critical payment processing operations.
What is “convention over configuration”?
Convention over configuration is a software development principlewhere the framework makes assumptions about the best way to configure things, reducing the need for developers to specify configurations explicitly. Instead, it follows a set of conventions that work by default.
Let’s say you’re creating a database table for a ‘products’ feature in your application. Instead of explicitly specifying every single detail about how the table is structured, the framework, following its conventions, assumes certain defaults. For instance, the table name should be ‘products,’ the primary key should be ‘id,’ and relationships between tables should follow specific naming conventions without having to explicitly state these details.
By adopting these conventions, developers can focus more on writing application-specific code rather than defining configurations.
What do subdirectory app/controllers and app/helpers do?
app/controllers help Rails find controller classes and essentially handle web requests from the user while app/helpers hold any helper classes that assist the controller, model, or view classes
What command can you use to create a controller for the subject?
rails generate controller Subject
Replace “Subject” with the name you want to use for your controller. This command creates a controller file, along with associated view and helper files, under the app/controllers
and app/views
directories respectively, following Rails’ conventions for MVC (Model-View-Controller) structure.
What can Rails Migration do?
- Schema Modification: Migrations can modify the database schema, allowing the creation, modification, or deletion of tables, columns, and indices. This is done using Ruby code, making it database-agnostic and ensuring consistency across different database systems.
- Version Control for Database: Migrations maintain a version history of database changes. They allow developers to revert changes, roll back to a specific version, and manage the database structure’s evolution over time, offering a systematic way to track and manage database changes.
- Seeding Data: Migrations can also be used to seed the database with initial or default data. This is helpful during the initial setup or when populating the database with default values required for application functionality, ensuring a consistent starting point for development or production environments.
What is the Rails Controller?
The controller acts as an intermediary between the user, who interacts with the application via the view (web interface), and the application’s database via the model.
Key functions of the Rails Controller include:
- Handling Requests: It receives incoming HTTP requests from the browser, processes the user’s input or interaction, and determines the appropriate action to take based on that request.
- Business Logic: The controller contains the application’s business logic, orchestrating the flow of data between the model (database) and the view (user interface). It fetches data from the model, processes it, and sends it to the view for presentation.
- Render Views: After processing the request, the controller renders the appropriate view to display the response back to the user, typically in the form of HTML, JSON, or other formats.
How can you protect Rails against Cross-Site Request Forgery?
CSRF Tokens: Rails provides built-in support for CSRF protection by generating an authenticity token for each session and embedding it in forms or AJAX requests. Upon submission, Rails compares the token in the request with the expected token stored on the server. If they don’t match, the request is rejected.
Use CSRF Meta Tags: Rails includes CSRF meta tags in views by default that automatically generate tokens for each request.
Verify Authenticity Token: In your controller, you can use the protect_from_forgery method to enforce CSRF protection for your entire application. This method validates the authenticity token in incoming requests.
What does garbage collection do in Ruby on Rails?
Garbage collection in Ruby on Rails, which is part of the Ruby programming language, is a process responsible for managing memory by identifying and freeing up memory that is no longer in use.
Name three of the limits of Ruby on Rails
Performance Scaling: While Ruby on Rails is excellent for rapid development, scaling Rails applications can require significant optimizations or additional components to handle massive user loads.
Learning Curve: Ruby on Rails has a learning curve, especially for newcomers to the framework or programming in general. While it promotes convention over configuration, it may take time to fully grasp the “Rails magic” and best practices.
Monolithic Structure: Ruby on Rails applications often follow a monolithic architecture, where all components are tightly integrated. This can make it less flexible for projects that require microservices or a more modular structure.
Name three advantages of using Ruby on Rails
Rapid Development: Ruby on Rails convention over configuration principle allows developers to build robust applications with less code, reducing development time and accelerating time-to-market.
Mature Ecosystem and Community Support: Ruby on Rails provides extensive documentation, a rich set of gems (Ruby libraries), and a supportive community that fosters learning and problem-solving.
Maintainability and Conventions: The framework’s structure and conventions help maintain code quality, reduce redundancy, and ensure maintainability as the application grows.
Name the four types of variables available in Ruby Class
Local Variables: These variables are confined within a method or a block and can’t be accessed outside their scope. They start with a lowercase letter or an underscore.
Instance Variables: Prefixed with the “@” symbol, these variables are associated with a particular instance of a class. They hold data that is unique to each instance of the class.
Class Variables: Identified by the “@@” prefix, class variables are shared amongst different objects of the same class. They are used to store data shared across all instances of a class.
Global Variables: Marked with the “$” symbol, global variables can be accessed from anywhere within the Ruby application. They persist throughout the application and should be used with caution to prevent unintended consequences due to global scope.
Understanding and using these different types of variables is crucial for effective data management and proper encapsulation within Ruby classes.