Skip to content

Instantly share code, notes, and snippets.

View Integralist's full-sized avatar
🎯
Making an impact

Mark McDonnell Integralist

🎯
Making an impact
View GitHub Profile
@Integralist
Integralist / Concurrency vs Parallelism.md
Created March 26, 2014 09:04
Concurrency vs Parallelism

This is my understanding of the difference between "Concurrency" and "Parallelism". I believe it's reasonably accurate - although feel free to discuss in the comments if you feel the distinction is different to my definition.

Effectively there is just "concurrency". Concurrency is the ability to handle multiple tasks/processes all running at the same time (rather than running tasks sequentially: one finishes, the next starts).

If there is only a single CPU available then concurrent processes won't perform as well as you might think because the CPU will be forced to do something called "task switching". Imagine you have two tasks (A and B) which are running concurrently; "task switching" breaks down to:

  • CPU works on task A for a short (predetermined) amount of time
  • If task A isn't complete by end of the set time frame then task B is started
  • CPU works on task B (again only for the predetermined amount of time)
  • If task B doesn't complete within the time frame then the CPU jumps back to work on task
@Integralist
Integralist / Hash Keys to Symbols.rb
Last active September 2, 2020 08:50
Convert Ruby Hash keys into symbols
hash = { 'foo' => 'bar' }
# Version 1
hash = Hash[hash.map { |k, v| [k.to_sym, v] }]
# Version 2
hash = hash.reduce({}) do |memo, (k, v)|
memo.tap { |m| m[k.to_sym] = v }
end
@Integralist
Integralist / 1. SRP .md
Created March 11, 2014 09:42
S.O.L.I.D principles in Ruby

Single responsibility principle

Probably the most well known principle, and one that should try to adhere to most of the time.

Let's say you have this code:

class AuthenticatesUser
  def authenticate(email, password)
    if matches?(email, password)
@Integralist
Integralist / GitHub curl.sh
Last active February 6, 2025 20:47 — forked from madrobby/gist:9476733
Download a single file from a private GitHub repo. You'll need an access token as described in this GitHub Help article: https://help.github.com/articles/creating-an-access-token-for-command-line-use
curl --header 'Authorization: token INSERTACCESSTOKENHERE' \
--header 'Accept: application/vnd.github.v3.raw' \
--remote-name \
--location https://api.github.com/repos/owner/repo/contents/path
# Example...
TOKEN="INSERTACCESSTOKENHERE"
OWNER="BBC-News"
REPO="responsive-news"
@Integralist
Integralist / reduce with key and value.rb
Last active August 29, 2015 13:56
Passing through key and value to a reduce method block rather than just the item
# As expected the output is the key, then the value...
({ :key => :value, :foo => :bar }).reduce([]) { |pass_through, item|
puts item
}
# key
# value
# foo
# bar
@Integralist
Integralist / << method.rb
Last active August 29, 2015 13:56
In Ruby using the `<<` operator as a method identifier has special meaning. It allows us to call the method without using a period (so we can do `foo << "abc"` and not have to do `foo.<< "abc"`)
class Foo
def <<(something)
puts something
end
def say(something)
puts something
end
end
@Integralist
Integralist / 1. Success.rb
Last active October 24, 2023 16:51
How to clone a Hash (in Ruby) and modify the cloned hash without affecting the original object
# This is the ONLY way I've found that works
# All other suggested solutions (see below examples) don't actually work
# And as an extra bonus: this deep copies as well!
def deep_copy(o)
Marshal.load(Marshal.dump(o))
end
@Integralist
Integralist / heredoc.rb
Created February 12, 2014 12:04
Ruby HEREDOC but not worrying about the crappy spacing
<<-FOO.gsub /^\s+/, ""
abc
def
ghi
jkl
FOO
@Integralist
Integralist / DependencyHelper.js
Last active February 3, 2018 04:55
Better Mocking using RequireJS' `undef` method to unset redefined modules
define(['require'], function(require) {
var stubbed = [];
return {
stub: function(name, implementation) {
stubbed.push(name);
requirejs.undef(name);
define(name, [], function() {
return implementation;
});
},