Skip to content

Instantly share code, notes, and snippets.

@kylekyle
kylekyle / parallel_search.rb
Created April 23, 2015 03:21
If you want to search for something in parallel in Ruby, the thread gem is the way to go
# gem install thread
require 'thread/pool'
require 'thread/channel'
require 'ruby-progressbar'
pool = Thread.pool 2
channel = Thread.channel
progress = ProgressBar.create totaL: 1_000_000
1_000_000.times do |i|
@kylekyle
kylekyle / spark.rb
Created June 1, 2015 02:30
An implementation of the first example in the Spark Quick Guide in jRuby
# the first example in the Spark Quick Start Guide in jRuby
# https://spark.apache.org/docs/latest/quick-start.html
require './spark-assembly-1.4.0-SNAPSHOT-hadoop2.4.0.jar'
conf = org.apache.spark.SparkConf.new
conf.setAppName('ruby!').setMaster('local')
sc = org.apache.spark.SparkContext.new conf
textFile = sc.textFile('README.md', 1)
@kylekyle
kylekyle / websockets.rb
Last active August 29, 2015 14:26
An example of creating a barebones web socket server and client in Ruby
require 'socket'
require 'websocket'
require 'websocket-client-simple'
port = rand(10_000..60_000)
thread = Thread.new do
begin
server = TCPServer.new port
socket = server.accept
@kylekyle
kylekyle / ruby-prompter.rb
Created September 21, 2015 23:43
Blocking bidirectional communication in IRuby
comm = IRuby::Comm.new("prompter")
comm.open
session = IRuby::Kernel.instance.session
message = session.instance_eval do
unserialize(@sockets[:reply].recv_message)
end
comm.close
@kylekyle
kylekyle / upload.rb
Last active December 8, 2015 03:39
jQuery upload to Sinatra
require 'slim'
require 'sinatra'
template :upload do
<<-END
head
script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"
javascript:
$(document).ready(function() {
$('#upload :button').click(function(){
@kylekyle
kylekyle / keyboard.rb
Last active December 11, 2015 20:48
Reads keypress information and translates to midi notes. See comments for details.
# This reads from key presses from a serial device (such
# as an arduino) in the following form:
# +4-2+11
# this means that the keys 4 and 11 are engaged
# and key 2 is not engaged. Use the keys map below
# to map keys to midi notes
require 'unimidi'
require 'serialport'
@kylekyle
kylekyle / piano.ino
Created January 28, 2013 21:12
This sketch determines which buttons are being pressed by checking how long a pin takes to pull high. See comments for details.
// This sketch is a trivial extension of:
// http://www.instructables.com/id/Turn-a-pencil-drawing-into-a-capacitive-sensor-for/
// This sketch returns which digital pins
// specified in the pins array take 50 or more
// cycles to pull high. Pins that take more than
// 50 cycles have a + and pins that take less
// have a -
int pins[] = {
2,3,5,6,8,9,11,12
};
@kylekyle
kylekyle / tree.json
Created January 23, 2016 03:37
Place in ~/.jupyter/nbconfig/ to make Ruby the default kernel
{
"NewNotebookWidget": {
"default_kernel": "ruby"
}
}
@kylekyle
kylekyle / custom.js
Last active January 23, 2016 05:28
More tricks
IPython.CodeCell.options_default.cm_config.autoCloseBrackets = false;
setTimeout(function() {
if (window.terminal != undefined) {
window.terminal.term.screenKeys = true;
}
}, 1000);
@kylekyle
kylekyle / status.py
Last active January 23, 2016 18:39
Return a health monitoring status
''' Add to jupyter_notebook_config.py
import sys
sys.path.append("/root/.jupyter/extensions/")
c.NotebookApp.reraise_server_extension_failures = True
c.NotebookApp.server_extensions = ['status']
'''
from notebook.utils import url_path_join
from notebook.base.handlers import IPythonHandler