Skip to content

Instantly share code, notes, and snippets.

@mtkd
mtkd / safe_navigator_cost.rb
Created December 25, 2015 11:56
Perf difference between try and safe navigator in Ruby 2.3.0
# Test perf difference between try and safe navigator in Ruby 2.3.0
require 'benchmark'
require 'active_support'
require 'active_support/core_ext/object'
Benchmark.bm do |x|
obj = 0
x.report { 10000.times do; obj.try(:to_s); end; }
x.report { 10000.times do; obj&.to_s; end; }
@mtkd
mtkd / super_defined_cost.rb
Created May 10, 2014 16:38
super vs super if defined?(super)
# Test to see how much time checking super defined costs (approx 10%)
require 'benchmark'
class Base
def meth; end
end
class WithSuperCheck < Base
def meth; super if defined?(super); end
@mtkd
mtkd / mongoid_deep_clone.rb
Last active August 29, 2015 14:01
Mongoid document deep clone and save
# without new_record set it silently doesn't save
new_document = Marshal.load(Marshal.dump(self))
new_document._id = Moped::BSON::ObjectId.new
new_document.new_record = true
new_document.save

Keybase proof

I hereby claim:

  • I am mtkd on github.
  • I am mtkd (https://keybase.io/mtkd) on keybase.
  • I have a public key whose fingerprint is 3B14 B113 0FDD 8519 4F62 3356 C531 D614 ABF8 DE15

To claim this, I am signing this object:

@mtkd
mtkd / database_cleaner_mongo_minitest_example.rb
Created January 18, 2014 10:28
Using DatabaseCleaner with Mongo Driver and Minitest
# not obvious from the database_cleaner documentation
# this is snippets of relevant parts, it's not meant to run
require 'mongo'
require 'database_cleaner'
require 'database_cleaner/mongo/base'
include Mongo
@mongo_client = MongoClient.new
@mtkd
mtkd / test_exception_message
Last active December 27, 2015 19:29
Test exception message
exc = assert_raises Mongoid::Errors::Validations do
subject.do_something
end
assert exc.message.include?("something failed")
@mtkd
mtkd / gist:5608396
Last active December 17, 2015 12:19
Install Sublime Text 2 settings
cd ~/Library/Application Support/Sublime Text 2/Packages
mv ./User/ ./User_old/
ln -s ~/Dropbox/Sublime/Packages/User ./User
# Edit this in Package/Color Scheme - Default/Twilight
# <key>lineHighlight</key>
# <string>#302030</string>
@mtkd
mtkd / mail_send_example.rb
Created March 17, 2013 12:55
Simple Ruby SMTP send example
require 'net/smtp'
message = <<MESSAGE_END
From: Sender <name@sender_domain.com>
To: Receiver <name@receiver_domain.com>
Subject: SMTP e-mail test
This is a test e-mail message.
MESSAGE_END
@mtkd
mtkd / gist:3729321
Last active October 10, 2015 17:58
Curl options
# return only HTTP status code
curl -sL -w "%{http_code}\\n" "http://news.ycombinator.com/" -o /dev/null
# return only HTTP status code and 302 URL
curl -sL -w "%{http_code} %{url_effective}\\n" "http://news.ycombinator.com/" -o /dev/null
# header only
curl -I news.ycombinator.com
# basic auth
@mtkd
mtkd / gist:3728976
Created September 15, 2012 17:34
ActiveModel::Serializer and :only
class FooSerializer < ActiveModel::Serializer
attributes :_id, :name
def attributes
h = super
h.select! { |x| options[:only].include? x } if options[:only]
h
end
end