Skip to content

Instantly share code, notes, and snippets.

@eljojo
eljojo / README.md
Created February 26, 2014 12:37 — forked from lucaong/README.md

ClosedStruct

A stupidly simple implementation of a kind of "sorta immutable" struct that can be instantiated as conveniently as an OpenStruct but after instantiation it doesn't let me (easily) add fields or mutate them:

guy = ClosedStruct.new( name: "John", age: 23 )

# readers:
guy.name             #=> "John"
guy.age #=> 23

Keybase proof

I hereby claim:

  • I am eljojo on github.
  • I am eljojo (https://keybase.io/eljojo) on keybase.
  • I have a public key whose fingerprint is 4ACE 2687 33CD 4307 D826 059F F1FA 10B3 6B3A 8FC7

To claim this, I am signing this object:

@eljojo
eljojo / bottles_of_beer.rb
Created March 31, 2014 19:47
learn ruby with beer
def sing(number)
puts "#{number} bottles of beer on the wall,"
puts "#{number} bottles of beer."
puts "Take one down, pass it around,"
puts "#{number - 1} bottles of beer on the wall."
end
99.downto(1).each do |number|
sing(number)
end
@eljojo
eljojo / loops_delay.ino
Created April 13, 2014 20:25
small program to teach my brother custom delays in arduino, using loops and measuring distance
int rojor = 2;
int blancof = 4;
int valiza = 5;
int parlante = 8;
int cuenta = 0;
int distancia = 0;
int estado = 0; // cuando estado == 1, esta prendido, == 0, esta apagado
//----------------------ARDUMOTO-------------------------//
#define derecha 0 //
@eljojo
eljojo / sidekiq_already_running.rb
Created April 16, 2014 12:01
See if a Sidekiq job is queued or running
class DaWorker
include Sidekiq::Worker
def perform
sleep(10)
end
class << self
def queue_name
sidekiq_options_hash["queue"].to_s || "default"
@eljojo
eljojo / simple_queue.rb
Created April 29, 2014 11:11
simple redis queue. it might have bugs.
class SimpleQueue
def initialize(name)
@name = name.to_sym
end
def push(items)
return unless items.present?
items = [items] unless items.is_a?(Array)
with_redis do |redis|
redis.sadd(redis_key, items.map(&:to_s))
@eljojo
eljojo / savings_per_second.rb
Created July 8, 2014 11:09
savings per year
$savings_per_year = 300.0
seconds_in_a_year = 3600 * 24 * 365
$money = $savings_per_year/seconds_in_a_year
elapsed_seconds = 0.0
loop do
puts("you just lost %.9f euros by not switching your electricity company" % ((elapsed_seconds += 1) * $money))
sleep 1
end
@eljojo
eljojo / using_ast_tree.rb
Last active August 29, 2015 14:04
Using the information extracted from an AST Tree, so we can do something meaningful with it.
# Using the information extracted from an AST Tree, so we can do something meaningful with it.
# this needs https://github.com/whitequark/parser
require 'parser/current'
op = "(a > 3) or (a == b) or ((b > 3.months) and (c =~ 'd')) or jojo?"
parsed = Parser::CurrentRuby.parse(op)
def clean_begin(node)
return node unless node.type == :begin
raise "begin has more than one children!" if node.children.length > 1
@eljojo
eljojo / almost_rbx.rb
Last active August 29, 2015 14:04
Almost Rubinius
module Rules
class Processor
def initialize(context)
require 'parser/current'
@context = context
end
def process(command)
node = node_for_command(command)
process_node(node)
@eljojo
eljojo / callback_example.js
Last active August 29, 2015 14:06
example of what a callback is
function loadTweets() {
// first we start loading the tweets with ajax and set the callback to tweetsLoaded
$.get("/tweets", tweetsLoaded)
// then, we show a message stating that we're loading the tweets
$("#status").text("loading tweets")
}
function tweetsLoaded(tweets) {