Procs Lambdas and Blocks

Blocks Blocks in ruby are anonymous functions that can be passed into methods. Calling Blocks Blocks can be called using the yield keyword or by calling .call, the latter is called an explicit block while the former is called an implicit block. explicit block: Using .call to execute a block def explicit_block(&block) block.call end explicit_block { puts "Explicit block called" } implicit block: using yield to execute a block # block passed as anonymous function def test_method yield end test_method { puts "Block is being run" } # block with parameters def one_two_three yield 1 yield 2 yield 3 end one_two_three { |number| puts number * 10 } # 10, 20, 30 Proc a proc is an instance of the Proc class and is similar to a block....

August 6, 2023 · 3 min · Ashish Gaur

Sessions in Rails

Sessions provide a way to store any kind of data across requests. This could be user info, preferences, cart items etc. Using sessions is fairly easy, Rails provides a session variable which is a dictionary with key-value pairs. # Writes login of the user in the session session[:user_login] = user.login # Reading from session user_login = session[:user_login] Since HTTP is a stateless protocol, sessions comes in handy in retaining info across subsequent requests....

August 6, 2023 · 2 min · Ashish Gaur

Concerns in Rails

Concerns allow us to include modules with methods (both instance and class) and constants into a class so that the including class can use them. Rails concerns are defined by extending ActiveSupport::Concern module. By extending this the module can define two blocks: included: Any code written inside this block is evaluated in the context of the including class. That means if we define a method in this block then that’ll be available to the class in which the concern is included....

August 6, 2023 · 2 min · Ashish Gaur

Basics of Scaling

In this blog we’ll learn the basics required to scale an application. Lets start with how does the architecture of a small application looks like. Basic API Architecture At the most basic level any application has following parts: Client - which requests the API and gets back a response. Server - which processes the request and sends back the response. Database - which stores any data that the server needs to process any subsequent requests....

December 27, 2022 · 6 min · Ashish Gaur

Minimum Spanning Tree

A minimum spanning tree(MST) is a subset of a graph which connects all the vertices with minimum total edge weight and least number of edges(no cycles). Formally a MST has following properties: Subset of the graph. Contains all vertices of the graph. Every vertex is reachable from all other vertices. Contains no cycles. Minimum total edge weight among all spanning tree. The last point is the defining property of an MST....

October 26, 2022 · 2 min · Ashish Gaur