Skip to content

Instantly share code, notes, and snippets.

@JudahSan
Last active January 24, 2023 10:08
Show Gist options
  • Save JudahSan/02e6e29c3ecefbf66736db85fc4b750d to your computer and use it in GitHub Desktop.
Save JudahSan/02e6e29c3ecefbf66736db85fc4b750d to your computer and use it in GitHub Desktop.
Ruby Asynchrony:

Asynchrony and synchrony are two different ways in which a computer program can execute its instructions.

Synchronous programming is the traditional way of writing code, where each instruction is executed one at a time, in the order in which it appears in the program. This means that if a blocking operation, such as a network request, is encountered, the program will block and wait for the operation to complete before moving on. This can lead to the program being unresponsive or slow if there are many blocking operations.

On the other hand, asynchronous programming allows a program to execute multiple instructions at the same time, without blocking the execution of other instructions. This means that if a blocking operation is encountered, the program can continue to execute other instructions while waiting for the operation to complete. This can lead to a more responsive and efficient program.

Asynchronous programming is often implemented using callbacks, promises, or async/await mechanisms, where a function is called and instead of waiting for it to finish and return a result, the function returns immediately, and a callback function is passed to it, which is called when the function completes its execution.

In summary, synchronous programming executes instructions one after the other, while asynchronous programming allows multiple instructions to be executed simultaneously, without blocking the execution of other instructions.

Ruby is a synchronous programming language by default, which means that each line of code is executed one at a time, and the program will not move on to the next line until the current line has completed execution. This means that if a blocking operation, such as a network request, is encountered, the program will block and wait for the operation to complete before moving on.

However, Ruby also has libraries and frameworks that provide support for asynchronous programming, such as EventMachine and Celluloid. These libraries allow you to write non-blocking code, so that the program can continue to execute other tasks while waiting for a blocking operation to complete.

Additionally, Ruby has Fibers which are a way to implement cooperative multitasking in ruby. They allow you to write code that can be paused and resumed without blocking the execution of the program.

In summary, while Ruby is synchronous by default, it has libraries and frameworks that allow for asynchronous programming.

My Adventure With Async Ruby by Matheus Richard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment