Skip to content

Instantly share code, notes, and snippets.

View maxdignan's full-sized avatar

Max Dignan maxdignan

View GitHub Profile
require 'Car'
class Driver
#...
end
def add(a, b)
c = a + b
end
def add_ten (a)
puts "10 is being added to your value"
b = a + 5
c = a + 10
end
module SpecialLogger
def log(value)
puts "Here's your value: #{value}"
end
module_function :log
end
SpecialLogger.log((1+1) / 2) #-> puts "Here's your value: 1"
module Talkable
def speak(word)
puts word
end
end
class MyClass
def some_method
...
require 'ostruct' #needed, but available in Ruby.
os = OpenStruct.new
os.method_name = "here's your value you want"
os.method_name #-> "here's your value you want"
class YourFirstClass
def initialize(word, num_a, num_b, num_c)
@word = word
@num_a = num_a
@num_b = num_b
@num_c = num_c
end
def hello(name)
"Hello #{name}"
@maxdignan
maxdignan / ExistentialQQ.rb
Last active June 3, 2016 18:29
Ruby Existential Check
class Object
def QQ
if !self.nil?
return self
end
Qe.new
end
end
class Qe
@maxdignan
maxdignan / FileStreaming.js
Created April 26, 2016 20:53
FileStreamingNode
var Iconv = require('Iconv').Iconv;
var fs = require('fs');
var start = Date.now();
function runner(i){
rStream = fs.createReadStream('enwik8');
var iconv = new Iconv('UTF-8', 'UTF-16LE');
wStream = fs.createWriteStream('enwik16');
return rStream.pipe(iconv).pipe(wStream);
@maxdignan
maxdignan / async_handler.rb
Last active January 10, 2016 01:45
Async handler to manage multiple db calls within a Rails Controller Action concurrently, without any hassle
def async_handler(*args)
dass = self
threads = []
args.each do |t|
threads << Thread.new do
ActiveRecord::Base.connection_pool.with_connection do
dass.send t
end
end
end