Skip to content

Instantly share code, notes, and snippets.

View hold_the_chocolate.rb
#!/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 Aug 29, 2015
The Ruby object model
View i_object.md

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 Jun 7, 2015
Demo bug using Array#product to build a hash
View tags.rb
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 Aug 29, 2015
ActiveRecord log silencing example
View rails_console.rb
$ 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 Aug 29, 2015
DB color distribution
View all_colors
# 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 Jun 4, 2018
Google Spreadsheet row insertion example
View add_row.sh
# $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 Oct 15, 2014
Promises in Angular
View promises.md

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?

View keybase.md

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:

View require_implementation.rb
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