Ruby On Rails Interview Question
Translate
Tuesday, 9 July 2024
Understanding the Differences: update, update_columns, update_column, update_attributes, assign_attributes in Rails
Sunday, 20 August 2023
Explain About Restful Architecture?
RESTful architecture, also known as REST (Representational State Transfer), is a design principle and architectural style commonly used in web development to design networked applications, particularly web services and APIs. It provides a set of guidelines for creating well-organized, scalable, and maintainable web applications that follow a client-server interaction pattern. REST is based on a few key principles that promote simplicity, scalability, and modularity.
Key principles of RESTful architecture:
1. **Client-Server Separation:** REST separates the client (user interface) and the server (data storage and processing), allowing each to evolve independently. This separation improves scalability and flexibility.
2. **Stateless Communication:** Each request from the client to the server must contain all the information necessary for the server to fulfill the request. The server doesn't store any client state between requests. This simplifies server design and enhances scalability.
3. **Resource-Based Identification:** Resources are the main entities in a RESTful architecture, and each resource is identified by a unique URL (Uniform Resource Locator). Resources represent data or services that can be accessed, manipulated, or modified through the API.
4. **HTTP Methods:** RESTful APIs use standard HTTP methods (GET, POST, PUT, DELETE, etc.) to perform operations on resources. Each HTTP method corresponds to a specific action on the resource (e.g., GET for reading, POST for creating, PUT for updating, DELETE for deleting).
5. **Uniform Interface:** REST enforces a uniform interface across different resources and applications. This consistency simplifies communication and allows clients and servers to evolve independently.
6. **Resource Representation:** Resources can have multiple representations, such as JSON, XML, HTML, or other formats. Clients and servers can negotiate the preferred representation using the `Accept` and `Content-Type` headers.
7. **HATEOAS (Hypermedia as the Engine of Application State):** This principle suggests that a client can navigate the application's resources using hyperlinks provided in the responses. This reduces the coupling between the client and server, as the client can discover available actions dynamically.
Benefits of RESTful architecture:
- **Simplicity:** RESTful APIs are straightforward and easy to understand due to their consistent use of HTTP methods and resource-based URLs.
- **Scalability:** Stateless communication and separation of concerns allow for efficient scaling of both clients and servers.
- **Flexibility:** Clients and servers can evolve independently, allowing updates without breaking existing clients.
- **Interoperability:** RESTful APIs can be consumed by a variety of clients, including web browsers, mobile apps, and other services.
- **Caching:** HTTP's caching mechanisms can be leveraged to improve performance and reduce server load.
- **Standardization:** REST follows established HTTP standards, making it widely supported and easy to integrate with various tools.
In summary, RESTful architecture provides a set of principles for designing web applications that promote simplicity, scalability, and modularity. It emphasizes resource-based URLs, standard HTTP methods, stateless communication, and a uniform interface, making it a popular choice for building web services and APIs.
Saturday, 19 August 2023
Difference between map and each, give one example for same output
In Ruby, both `map` and `each` are used to iterate over collections, but they have different purposes and behaviors.
1. **`each`:**
The `each` method is an iterator that allows you to loop through each element of a collection and perform an action with each element. However, `each` doesn't transform or modify the original collection; it's mainly used for its side effects.
```ruby
numbers = [1, 2, 3, 4, 5]
numbers.each do |num|
puts num * 2
end
```
In this example, the `each` loop doubles each element in the `numbers` array and prints the result. The original array remains unchanged.
2. **`map`:**
The `map` method is also an iterator, but it creates a new array by applying a block of code to each element of the original collection. It's used to transform the elements and return a new array.
```ruby
numbers = [1, 2, 3, 4, 5]
doubled_numbers = numbers.map do |num|
num * 2
end
```
In this example, the `map` method doubles each element in the `numbers` array and returns a new array called `doubled_numbers`. The original array remains unchanged.
Here's an example that demonstrates the difference between `each` and `map` using the same output:
```ruby
numbers = [1, 2, 3, 4, 5]
# Using 'each' to print doubled values
numbers.each do |num|
puts num * 2
end
# Using 'map' to create a new array with doubled values
doubled_numbers = numbers.map do |num|
num * 2
end
puts doubled_numbers.inspect
```
In this example, both the `each` loop and the `map` method produce the same output (doubled values of the original numbers), but the key difference is that `each` only performs an action (printing), while `map` transforms the data and creates a new array.
Sunday, 5 March 2023
Here is a 30-day plan to help you prepare for a Ruby on Rails interview:
Week 1:
Day 1-2: Refresh your knowledge of Ruby programming language fundamentals, including object-oriented programming concepts, data types, control flow, and loops.
Week 2:
Week 3:
Week 4:
Monday, 14 January 2019
what is Forwardable module
For example, say you have a class RecordCollection which contains an array @records. You could provide the lookup method record_number(), which simply calls [] on the @records array, like this
class RecordCollection
extend Forwardable
def_delegator :@records, :[], :record_number
end
Sunday, 6 March 2016
SQL Query to second find highest salary of Employee
n = 1 second highest salary
n= 2 third highest salary
SELECT DISTINCT(Salary) FROM Employees ORDER BY Salary DESC LIMIT n,1