Skip to content

Instantly share code, notes, and snippets.

View madis's full-sized avatar
🧑‍💻
code mode

Madis Nõmme madis

🧑‍💻
code mode
View GitHub Profile
@madis
madis / thread_explosion.rb
Created September 7, 2016 13:53
Testing ruby loading in multiple threads
require 'thwait'
$LOAD_PATH << '/Users/madisnoemme/code/etventure/rbn-be-api'
threads = 10000.times.map {
print "."
Thread.new {
require_relative './rbn/service/xml/mapping'
require_relative './rbn/service/xml/mappings'
}
}
def build_tree(root_id:)
root_row = db_find(id: root_id)
root_node =
{
id: root_id,
text: root_row[:text]
}
add_children(root_node, db_find_children(parent_id: root_id))
root_node
end
@madis
madis / flatten.rb
Created October 16, 2016 22:51
Flatland
def flatten(input, output=[])
input.each do |e|
if e.is_a? Array
output += flatten(e)
else
output.push e
end
end
output
end
@madis
madis / copy_over_ssh.sh
Created October 20, 2016 09:47
Copy multiple small files from remote machine using ssh & tar
# From http://meinit.nl/using-tar-and-ssh-to-efficiently-copy-files-preserving-permissions
ssh user@machine-where-precious-data-is "tar czpf - /some/important/data" | tar xzpf - -C /new/root/directory
#!/usr/bin/env ruby
class GraphGenerator
# Returns list of edges for n-vertex complete graph
#
# Example:
# GraphGenerator.generate(4)
# [1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]
def self.generate(count)
if count == 1
@madis
madis / serve.rb
Last active July 4, 2017 10:03
Simple Webrick server
#!/usr/bin/env ruby
require 'webrick'
class Servlet < WEBrick::HTTPServlet::FileHandler
PORT = 4200
INDEX_FILE = 'index.html'
ROOT_PATH = File.expand_path(__dir__) # Folder of current file
def do_GET(request, response)
@madis
madis / clojars_org_dependencies.md
Created August 20, 2017 08:24
CodeMode with Madis clojars.org dependencies
@madis
madis / bash_movement.md
Created September 1, 2017 05:47
Bash movement and delete by word
@madis
madis / palindrome.rb
Created December 28, 2018 11:14
Palindrome
#!/usr/bin/env ruby
# You can try the running code at http://tpcg.io/FnffuR
# More explicit implementation. The algorithm you described:
# iterating over characters from both ends of string.
# Returning false as soon as first mismatch is found
def palindrome_by_manual_reversing?(word)
word_string = word.to_s
# Return early because a word is always a palindrome if
@madis
madis / callback_server.rb
Last active March 14, 2019 12:38
Simple Webrick server for testing callbacks in ruby tests
require 'webrick'
require 'rack'
require 'rack/query_parser'
class CallbackServer
ENDPOINT = '/callbacks'
attr_reader :requests
def initialize