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 / 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 / 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 / AOP.md
Last active August 29, 2015 13:57
AOP (Aspect-Oriented Programming)

What is AOP?

Aspect Oriented Programming is a means to change the behaviour of – or add behaviour to – methods and functions (including constructors) non-invasively. The added behaviour is called “advice” and can be added before, after, or around the function it advises.

This description is similar to the Extract Surrounding refactoring method. The difference is in the direction of the change. It seems AOP is more focused at modifying existing behaviour non-invasively; where as the Extract Surrounding Method actually changes the source code to allow this type of behavioural modification.

Libraries

@Integralist
Integralist / Bad Design.rb
Last active August 29, 2015 13:57
Refactoring Ruby -> not all conditionals can be removed, and those that can can't necessarily use the standard refactoring methods such as "Replace Type Code with Module Extension", "Replace Type Code with Polymorphism" or "Replace Type Code with State/Strategy". The below examples demonstrate this.
class Foo
def initialize(a=[], b=[])
@a = a
@b = b
end
def add(item)
if item.is_a? A
@a.push item
else
@Integralist
Integralist / vim-plugins.sh
Last active August 29, 2015 13:58
For up to date list of plugins please see my Fresh Install repository: https://github.com/Integralist/Fresh-Install/
cd "$HOME/.vim/bundle"
plugins=( airblade/vim-gitgutter \
ap/vim-css-color \
bling/vim-airline \
edkolev/tmuxline.vim \
ervandew/supertab \
gcmt/wildfire.vim \
godlygeek/tabular \
kien/ctrlp.vim \
@Integralist
Integralist / 0. Clojure scratch pad.md
Last active August 29, 2015 14:00
Expanding scratch pad of Clojure code

What we cover:

  • defining functions
  • anonymous functions
  • short hand anonymous functions
  • complex reducing
  • alternatives to the reduce example
  • demonstrate how vectors and lists are different
  • custom method that separates the specified predicate from a sequence
  • destructuring with let bindings
@Integralist
Integralist / Ruby Array Guarding.md
Created May 9, 2014 07:32
Ruby Array Guarding

Below is an example of trying to protect against a request for data failing to return the expected data structure (in this case an Array)...

expect_an_array_back = SomeClass.make_a_request_for_data

if expect_an_array_back.any?
  expect_an_array_back.each do |user|
    # ...
  end
end
@Integralist
Integralist / 1. Tree
Last active August 29, 2015 14:02
Best way to modular Grunt tasks
|— Gruntfile
|— package.json
|— grunt
| – contrib-requirejs.js
@Integralist
Integralist / Auto Currying JavaScript Function.js
Created June 27, 2014 22:24
Auto Currying JavaScript Function
// Copied from http://javascriptweblog.wordpress.com/2010/06/14/dipping-into-wu-js-autocurry/
var autoCurry = (function () {
var toArray = function toArray(arr, from) {
return Array.prototype.slice.call(arr, from || 0);
},
curry = function curry(fn /* variadic number of args */) {
var args = toArray(arguments, 1);
return function curried() {