Skip to content

Instantly share code, notes, and snippets.

View Rojo's full-sized avatar
🏠
Working from home

Rojo

🏠
Working from home
  • México
View GitHub Profile
@Rojo
Rojo / require_relative_with_tco.rb
Created June 11, 2018 22:56 — forked from havenwood/require_relative_with_tco.rb
A #require_relative_with_tco that's like #require_relative but with tail call optimization
module Kernel
def require_relative_with_tco file
absolute_path = File.absolute_path file, __dir__
realpath = File.realpath "#{absolute_path.chomp '.rb'}.rb"
if $LOADED_FEATURES.include? realpath
false
else
RubyVM::InstructionSequence.compile_file(
realpath,
@Rojo
Rojo / convert
Last active June 7, 2018 21:11 — forked from ahadyekta/convert
Bash: Convertir un grupo de archivos JPG a WEBP
#First install the webp converter by this
sudo apt-get install webp
#go inside the directory where all images are there
#make sure all images are in RGB color mode, otherwise you will get error for CMYK images.
#Convert all images to RGB by this command (you should install ImageMagik to do that)
for f in *.jpg; do convert -colorspace RGB "$f" "${f}"; done
#finally convert all images to Webp format
@Rojo
Rojo / benchmark.rb
Last active June 6, 2018 03:41
Max Unit Submatrix
require 'benchmark'
require_relative 'lib/submatrix'
matrix = [
[1, 1, 0, 0, 1, 1, 0, 0, 1, 0],
[1, 0, 0, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 1, 1, 0, 1, 0, 1, 1, 1],
[1, 1, 1, 1, 1, 0, 0, 0, 0, 1],
[0, 0, 0, 1, 1, 1, 1, 0, 1, 1],
@Rojo
Rojo / default_hash.rb
Last active June 6, 2018 03:37
Muestra de una función que llena ciertas llaves de un `Hash` con valores por defecto cuando están vacias.
DEFAULT_VALUES = { temperature: 10, altitude: 1200, pressure: 500 }
def fill_default_values(current_values)
{}.tap do |values|
DEFAULT_VALUES.each_key do
|key| values[key] = current_values[key] || DEFAULT_VALUES[key]
end
end
end
@Rojo
Rojo / getpid.sh
Last active June 6, 2018 03:41
bash + awk with backticks
!/bin/bash
PID=`ps -eo pid,args | awk '/[[:digit:]]+ $1/ { print $1 }'`
echo $PID
@Rojo
Rojo / custom_exception_test.rb
Last active August 29, 2015 14:27
MiniTest: Assert custom exception (ERROR)
require 'minitest/autorun'
class MyError < StandardError
end
class MyExplorationsTest << Minitest::Test
def test_raises_my_exception
proc { fail MyError }.must_raise MyError
end
end
@Rojo
Rojo / suma_cubos.rb
Last active June 6, 2018 03:48 — forked from edymerchk/suma_cubos.rb
Para los números del 0 al 999, realizar la suma de los cubos de los digitos que los componen.
# abc=a^3+b^3+c^3
i = 0
max = 999
while i <=999
puts i.to_s.rjust(3,'0') if i == ((i /100) ** 3) + ((i % 100 / 10) ** 3) + ((i % 10) ** 3)
i +=1
end
@Rojo
Rojo / notification_object.json
Created December 18, 2014 18:33
Chain's New Block Notification
{
"id": "38220-243858-3848303838",
"state": "enabled",
"url": "https://username:[email protected]",
"type": "new-block",
"block_chain": "bitcoin"
}
@Rojo
Rojo / chain.rb
Created December 16, 2014 22:13
Chain Notifications
require 'sinatra'
require 'json'
post '/' do
body = request.body.read
result = JSON.parse(body)
if result['payload']['type'] == 'address'
status(200)
body('OK\n')
else
def print_out_specific_level(level)
traverse_specific_level(root_node, 1, level)
end
def traverse_specific_level(node, current_level, desired_level)
if (current_level == desired_level)
puts node.val
return
end