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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# As expected the output is the key, then the value... | |
({ :key => :value, :foo => :bar }).reduce([]) { |pass_through, item| | |
puts item | |
} | |
# key | |
# value | |
# foo | |
# bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
def <<(something) | |
puts something | |
end | |
def say(something) | |
puts something | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<<-FOO.gsub /^\s+/, "" | |
abc | |
def | |
ghi | |
jkl | |
FOO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(['require'], function(require) { | |
var stubbed = []; | |
return { | |
stub: function(name, implementation) { | |
stubbed.push(name); | |
requirejs.undef(name); | |
define(name, [], function() { | |
return implementation; | |
}); | |
}, |