Ruby & RoR Flashcards
What are some advantages of using Ruby?
Easy to learn: Ruby has a clean and simple syntax that is easy to read and write, making it a great language for beginners. It has a low barrier to entry and can be learned quickly.
Productivity: Ruby is designed to maximize developer productivity. It has a lot of built-in features and libraries that make common tasks quick and easy to perform. Additionally, its elegant syntax and concise code can help developers write code faster and with fewer errors.
Flexibility: Ruby is a highly flexible language that can be used for a wide range of applications. It is a general-purpose language that can be used for web development, mobile development, desktop applications, and more.
Scalability: Ruby is a scalable language that can handle large and complex applications. It has built-in support for concurrency, which allows it to handle multiple requests at the same time without slowing down.
Can you call a private method outside a Ruby class using its object?
No, you cannot call a private method outside a Ruby class using its object. Private methods in Ruby can only be called from within the class or module where they are defined. They cannot be called using an object of the class, even if that object was created from outside the class.
Explain the MVC pattern.
The MVC (Model-View-Controller) pattern is a design pattern used in software engineering to separate the concerns of an application into three interconnected components: the Model, the View, and the Controller. This pattern is commonly used in web development frameworks like Ruby on Rails and Django.
Model: The Model represents the data and business logic of the application. It encapsulates the data and provides methods to access and manipulate it. It acts as the source of truth for the application's data. View: The View represents the presentation layer of the application. It is responsible for displaying the data to the user in a user-friendly way. It receives input from the user and sends it to the Controller for processing. Controller: The Controller acts as the intermediary between the Model and the View. It receives input from the user via the View, processes it using the Model, and updates the View with the results. It contains the application logic and coordinates the interaction between the Model and the View.
How should you use content_for and yield?
content_for and yield are two important methods in Ruby on Rails that are used to implement dynamic content in views.
content_for is used to define a block of content that can be used later in the view. It takes a name as an argument and a block of content as its value.
The yield method is used to display the content that was defined with the content_for method.
Explain the role of modules and mixins in Ruby.
A module is a collection of methods and constants that can be included in a class using the include keyword. Modules can be thought of as libraries of reusable code that can be mixed into any number of classes, providing a way to share common functionality across different parts of an application. Modules can also define namespaces, allowing for better organization and encapsulation of code.
Mixins, on the other hand, are a specific use case of modules. They allow developers to add behaviour to a class without the need for inheritance. Mixins are included in a class using the include keyword and can add methods, constants, and other behaviour to the class. This allows for a more flexible and modular approach to object-oriented programming since classes can be composed of multiple mixins that each provide a specific set of functionality.
The main role of modules and mixins in Ruby is to enable code reuse and promote modular design. By breaking down complex functionality into smaller, more focused modules, developers can create reusable building blocks that can be easily combined and reused in different contexts. This can lead to more efficient and maintainable code since common functionality only needs to be implemented once and can be easily shared across different parts of an application.
Describe the difference between class and instance variables.
In object-oriented programming, both class and instance variables are used to store data that belongs to an object of a particular class. However, there are important differences between the two.
A class variable is a variable that belongs to the class itself, rather than to any individual instance of the class. Class variables are declared at the class level and are accessible to all instances of the class. When a class variable is modified, the change is reflected in all instances of the class. Class variables are typically used to store data that is common to all instances of a class, such as a count of the total number of instances created.
An instance variable, on the other hand, belongs to a particular instance of a class. Instance variables are declared inside the class, but outside of any methods, and are accessible only to that instance of the class. When an instance variable is modified, the change affects only that particular instance, and not any other instances of the class. Instance variables are typically used to store data that is specific to each instance of a class.
What are your favourite tools for writing unit tests and why?
There are several popular tools for writing unit tests in Ruby on Rails and Developers may have their own preferences for testing tools based on their specific needs, but these are some of the popular tools used for testing in Ruby on Rails.:
RSpec: RSpec is a popular testing framework for Ruby on Rails that provides a domain-specific language (DSL) for writing expressive and readable tests. It includes features such as support for mocking and stubbing, and provides a range of matchers for testing assertions. MiniTest: MiniTest is a lightweight testing framework that is built into Ruby and is included with Ruby on Rails. It includes features such as support for parallel testing, and provides a range of assertions for testing common cases. Shoulda: Shoulda is a testing framework for Ruby on Rails that is built on top of MiniTest. It includes features such as support for context-specific testing, and provides a range of matchers for testing assertions. Capybara: Capybara is a library for testing web applications that provides a DSL for simulating user interaction with a web page. It includes features such as support for browser automation, and provides a range of selectors for finding elements on a page.
What is unit testing (in classical terms)?
In classical terms, unit testing refers to the process of testing individual units of code in isolation to ensure that they are working as intended. In Ruby on Rails, unit testing typically involves writing tests for models, controllers, and other parts of the application.
Unit tests in Ruby on Rails are typically written using a testing framework such as RSpec or MiniTest. These tests are designed to be run automatically as part of a continuous integration (CI) process to ensure that code changes do not introduce new bugs or regressions into the application.
In Ruby on Rails, unit testing typically involves writing tests that simulate different scenarios and edge cases, such as creating and updating records in the database, handling validation errors, and responding to different HTTP requests. By testing these scenarios in isolation, developers can ensure that each unit of code is functioning correctly and that the overall application works as intended.