Skip to content

Instantly share code, notes, and snippets.

View kristjan's full-sized avatar

Kristján Pétursson kristjan

View GitHub Profile
#!/usr/bin/env ruby
# Automatically convert most of the syntax differences between Mocha and RSpec
# Mocks.
#
# Does not handle:
# - Multiline statements like `Foo\n.expects`
# - `with` being given a block
# - Probably other things
#
@kristjan
kristjan / i_object.md
Last active August 29, 2015 14:28
The Ruby object model

I, Object!

This post is adapted from my answer to this Stack Overflow question. If you want to start from the beginning of the universe and build out, go read that one. If you prefer to start with something you can touch and work backwards, here we go.

Ruby likes ducks. Which is to say that when we're coding, and we have an object, we don't particularly care what kind of object it is, so long as it responds to the messages we send it. It might be a Duck or a Child or a

@kristjan
kristjan / tags.rb
Created June 7, 2015 19:07
Demo bug using Array#product to build a hash
tags = %w[tag1 tag2 tag3]
h = tags.product([[]]).to_h
#=> {"tag1"=>[], "tag2"=>[], "tag3"=>[]}
h['tag1'] << 1
#=> [1]
h
#=> {"tag1"=>[1], "tag2"=>[1], "tag3"=>[1]}
@kristjan
kristjan / rails_console.rb
Last active August 29, 2015 14:21
ActiveRecord log silencing example
$ rails c
Loading development environment (Rails 4.1.2)
[1] pry(main)> Specialist.find(15)
Specialist Load (0.9ms) SELECT "specialists".* FROM "specialists" WHERE "specialists"."id" = $1 LIMIT 1 [["id", 15]]
=> #<Specialist id: 15, ...>
[2] pry(main)> ActiveRecord::Base.logger.silence(Logger::FATAL) { Specialist.find(16) }
=> #<Specialist id: 16, ...>
@kristjan
kristjan / all_colors
Last active August 29, 2015 14:21
DB color distribution
# All colors on all applications
$ for app in `heroku apps | tail -n +2`; do heroku config --app $app | cut -d':' -f1 | grep HEROKU_POSTGRESQL | cut -d_ -f3; done | sort | uniq -c | sort -rn
4 WHITE
3 CHARCOAL
2 YELLOW
2 ORANGE
2 ONYX
2 CRIMSON
2 COBALT
2 BRONZE
@kristjan
kristjan / add_row.sh
Last active June 10, 2022 09:10
Google Spreadsheet row insertion example
# $auth from auth.sh
# $spreadsheet_id from get_spreadsheets.sh
# $worksheet_id from get_worksheets.sh
curl \
--header "Authorization: GoogleLogin auth=$auth" \
--header 'Content-Type: application/atom+xml' \
-d @data.xml \
-X POST \
"https://spreadsheets.google.com/feeds/list/$spreadsheet_id/$worksheet_id/private/full"
# Example data in data.xml
@kristjan
kristjan / promises.md
Created October 15, 2014 03:06
Promises in Angular

Disclaimer: None of this code was actually run!

What's a Promise

A way to handle something that happens asynchronously. Instead of getting your result back, you get a promise back that you can handle in a success way and a failure way. At some point in the future, the promise resolves to one of those cases, passing your success/failure functions the applicable data.

Why use promises?

@kristjan
kristjan / keybase.md
Created September 23, 2014 17:46
Keybase proof

Keybase proof

I hereby claim:

  • I am kristjan on github.
  • I am kristjan (https://keybase.io/kristjan) on keybase.
  • I have a public key whose fingerprint is 90B8 10A8 031E E111 893B 49B4 9455 B368 7480 A7AA

To claim this, I am signing this object:

module RequireImplementation
def require_implementation(method_name)
define_method method_name do
raise NotImplementedError, "#{self.class.name} must implement #{method_name}"
end
end
end
class TestClass
alert('hi!')