Skip to content

Instantly share code, notes, and snippets.

View armw4's full-sized avatar
🎯
Focusing

Antwan R. Wimberly armw4

🎯
Focusing
  • Stay tuned...
  • Atlanta, GA, USA
View GitHub Profile
@armw4
armw4 / array-constructor-new-with-block.md
Last active August 29, 2015 13:56
A bit of syntactic sugar the Array contructor exposes to create a fixed number of instances.

by Antwan Wimberly

I was not aware this was possible until I came across this post looking for examples of using ActiveRecord and rspec in unison.

I frequently make use of sample data in my posts; most commonly in the form of arrays.

In the midst of "shooting the breeze" with my fellow companions on the awesome "programmer files" gist, I could have made use of:

Array.new(3) { Person.new }
@armw4
armw4 / shell-history-trickshot.md
Last active August 29, 2015 13:56
A handy bash/zsh/shell trick shot I picked up a few weeks back.

by Antwan Wimberly

I'm a huge fan and heavy user of Vim (I do all my ruby and open source development there...anything that' not Microsoft basically). I happen to use what I like to refer to as a "Vim bootstrap" in Steve Francia's awesome spf13.

It has sane defaults and comes equipped with the latest and greatest Vim plugins on the market.

After installing the bootstrap, you'll want to ensure you keep it update to date just like Homebrew formulas, ruby gems, and npm packages. It's prett easy to upgrade your bootstrap, but who really has enough RAM in their brain to store that curl command? Sure you could drop it into a script, make it executable via chmod, and put it into some folder that's attached to your $PATH variable, but I've yet to cross that bridge. I do know however that I've executed that command numerous times and that it should be stashed somewhere in my zsh history. I know it contains the string "curl", so I whip

@armw4
armw4 / file.join.md
Last active August 29, 2015 13:56
What happens when you invoke File.join with variable arguments?

by Antwan Wimberly

I finally figured it out (probably could have just ready the documentation, but who has time for that when they're gluttonously binging on source code)?

"You're eating right now, aren't you? You disgust us..."

It's just a platform independant way of creating a path. It's abstracting the proper separator for you (which is either a backslash or forward flash depending on whether or not you're on *nix or Windows). It's even smart enough to not add a slash to the path if any of your arguments already contain it.

File.join("hi-there/now", 'locales', '*.yml') # => "hi-there/now/locales/*.yml"
@armw4
armw4 / before-after-all-helper.coffee
Last active August 29, 2015 13:56
jasmine_node beforeAll afterAll helper
# monkey patches jasmine (node) to provide beforeAll and afterAll hooks
#
# be sure to add this file as a helper as described in my answer on SO:
#
# http://stackoverflow.com/a/22054113/389103
#
# inspired by https://groups.google.com/forum/#!msg/jasmine-js/1LvuiUPunwU/SXPo6PMGSSMJ
#
# targets jasmine-node@1.13.1 and jasmine@1.3.1
#
@armw4
armw4 / $httpBackend-url-matcher-builder.coffee
Last active August 29, 2015 14:15
Naive regex builder that helps henerate url matchers for $httpBackend based on routing syntax.
# https://docs.angularjs.org/api/ngMockE2E/service/$httpBackend
### @usage
path = require './path'
urlMatcher = path '/users/:guid/helloworld/:guid'
phones = [{name: 'phone1'}, {name: 'phone2'}]
$httpBackend.whenGET(urlMatcher).respond(phones)

Why?

Working with streams in node can be a scary and daunting task. The first time I saw the stream handbook, it left me quite perplexed. After seeing stuff like through, through2, and @acrute doing some stream kung fu on the job, I knew it was time. Streams are actually pretty straightforward once you get the hang of things. Read the handbook and the node docs and eventually your epiphany shall be born. I think one of the most confusing things for me was that Readable streams are for reading values that are produced by you, while Writeable streams are for dealing with values that are to be consumed by you. Say I wanted to put (write) some data from another stream into a file, and I wanted to take control of this process. Or say I just wanted to log (write) the results of a stream to say the console. These would be prime for Writeable

@armw4
armw4 / javascript-arguments-semi-demystified.md
Last active December 27, 2015 21:19
Array.prototype.slice is a widely used function in JavaScript. It's typically used when you've got a variable list of arguments (VARARGS...popular in ruby *). The reason you always see slice invoked with 0 is because that's the start index. Think of substring if that makes more sense.

###by Antwan Wimberly###

We'll start with a function that logs the output of calls to Array.prototype.slice; starting from 0 up until n (number of agruments).

function logArgs () {
  var length = arguments.length, args;

  for(var index = 0; index < length; index++) {
 args = Array.prototype.slice.call(arguments, index);
@armw4
armw4 / ruby-instance-variables.md
Last active December 31, 2015 11:28
Setting instance variables in ruby is all about the current value of self. Just because you use @foo = bar...doesn't mean you'll get a new copy of foo for every instance of your class.

###by Antwan Wimberly###

class HiO
  def print_i
    # note...this won't print 12...even though we set
    # this variable in the `toast` method
    puts "my var is #{@var}"

    # and neither will this...failure!!!
@armw4
armw4 / mixins.md
Last active December 31, 2015 14:29
Ruby is such an incredible programming language. One of the cool things I like about it is mixins. You can declaratively add behavior to a class in a very ad-hoc manner. It's so cool....again! One great opportunity I can think of for mixins is when you need to opt out of a base class's functionality in a very DRY (Do No Repeat Yourself...aka...n…

###by Antwan Wimberly###

class Base
  def print_value(value)
    puts value.to_s
  end
end

class DerivedClass < Base; end
@armw4
armw4 / binary-logarithm.md
Last active December 31, 2015 14:29
A little brain teaser for you my dear apprentice...

###by Antwan Wimberly###

Binary logarithm of some number y is some number x, where x is 2 raised to x

2 ^ x = y

x | y
-----
0 | 1