Skip to content

Instantly share code, notes, and snippets.

View leandronsp's full-sized avatar

Leandro Proença leandronsp

  • Brazil
View GitHub Profile
@leandronsp
leandronsp / checker_test.rb
Created December 20, 2022 23:25
Purchases Checker
require 'test/unit'
require 'digest'
require 'date'
class PurchasesChecker
attr_reader :authorizations
def initialize
@authorizations = []
@purchases_idx = {}
@leandronsp
leandronsp / bitwise_sum.rb
Created September 6, 2022 03:21
Overengineering sum in Ruby
class Integer
def method_missing(m, right)
return if m != :sum
bitwise_sum(self, right)
end
private
def bitwise_sum(left, right)
@leandronsp
leandronsp / 001-README.md
Last active December 27, 2023 12:04
OOP in Bash script

OOP in Bash script

Wait, what?

Inspired by this awesome article.

Prelude about OOP

According to wikipedia, OOP is a programming paradigm or technique based on the concept of "objects". The object structure contain data and behaviour.

Data is the object's state, which should be isolated and must be private.

@leandronsp
leandronsp / 001-server.bash
Last active May 4, 2024 06:32
A complete yet simple Web server (with login & logout system) written in Shell Script
#!/bin/bash
## Create the response FIFO
rm -f response
mkfifo response
function handle_GET_home() {
RESPONSE=$(cat home.html | \
sed "s/{{$COOKIE_NAME}}/$COOKIE_VALUE/")
}
@leandronsp
leandronsp / grafana-dashboard.json
Created July 6, 2022 21:23
Grafana Dashboard (basic)
{
"annotations": {
"list": [
{
"builtIn": 1,
"datasource": {
"type": "grafana",
"uid": "-- Grafana --"
},
"enable": true,
@leandronsp
leandronsp / add-index.sql
Created June 11, 2022 02:54
Comparing B-Tree index vs CTE's
DROP INDEX IF EXISTS transfers_in, transfers_out;
CREATE INDEX transfers_in ON transfers (target_account_id);
CREATE INDEX transfers_out ON transfers (source_account_id);
@leandronsp
leandronsp / ruby101.rb
Created June 4, 2022 20:49
Entendendo attr_accessor, writer e reader no Ruby
class Dog
def name
# retorna variável de instância
@name
end
end
doguinho = Dog.new
# Chamando método de instância
@leandronsp
leandronsp / server.rb
Created April 6, 2022 01:40
Web Login/Logout in Ruby, using TCP socket, in less than 110 lines
require 'socket'
require 'cgi'
PORT = 3000
socket = TCPServer.new('0.0.0.0', PORT)
puts "Listening to the port #{PORT}..."
loop do
# Wait for a new TCP connection..."
@leandronsp
leandronsp / cractor.rb
Created January 17, 2022 01:14
A dead simple Actor in Ruby, simulating the same API for Ractors
class Cractor
def initialize(*args, &block)
@inbox = Queue.new
@outbox = Queue.new
Thread.new do
result = yield(self, *args)
self.yield(result)
end
@leandronsp
leandronsp / palindromesX.js
Last active January 4, 2022 12:29
Palindromes of X random words
/*
Reverse a word
*/
const reverse = (word) => {
const letters = word.split('')
const lastIdx = letters.length - 1
return letters
.map((letter, idx) => { return letters[lastIdx - idx] })
.join('')