Skip to content

Instantly share code, notes, and snippets.

@choallin
choallin / callbacks_register.rb
Last active October 31, 2019 15:19
Event loop with callback register class
class CallbackRegister
def initialize
@callbacks = {read: [], write: []}
end
def each(type, &block)
@callbacks[type].each do |callback|
yield callback
end
def process_partial_file_input(data)
# Do something cool with data
end
# Event loop
big_file = "network/path/big_file.xlsx"
file_handler = open(big_file)
files_to_look_after = [file_handler]
@choallin
choallin / funny_function.js
Created October 8, 2019 20:27
funny_function
function x() {
  alert(x);
  var x = "Interesting…"
}
@choallin
choallin / function_scoping.js
Created October 8, 2019 20:25
function_scoping
function x() {
  if(true) {
  var x = "Interesting"
  }
  alert(x);
}
@choallin
choallin / dynamical_scoping.rb
Last active October 8, 2019 20:21
dynamical_scoping
def multiply_by_two
  multiplicand = 2
  test_method
end
def multiply_by_three
 multiplicand = 3
  test_method
end
@choallin
choallin / lexical_scoping.rb
Created October 8, 2019 15:41
lexical_scoping
def test_method
 method_variable = 4
 different_variable = [1,2,3]
 different_variable.map do |number|
  number * method_variable
  end
end
class DoorLock
def initialize(locked)
@locked = locked
@mutex = Mutex.new
end
def open?
@mutex.synchronize { !@locked }
end
4.times.map do
t = Thread.current
t["#{dynamic_variable_name}".to_sym] = method_call
end
joined_threads = threads.map &:join
joined_threads.each do |t|
# The keys method returns all thread local variables by names
# analog to the Hash#keys method
t.keys.each do |key|
class DoorLock
def initialize(locked)
@locked = locked
end
def open?
!@locked
end
def unlock!
resource_a = []
threads = []
threads << Thread.new do |t|
50.times do |i|
resource_a << i
end
end
threads << Thread.new do |t|