Skip to content

Instantly share code, notes, and snippets.

class YOLO
attr_reader :i,
:want,
:to,
:die,
def really
:die
end
end
class FakeHash
def to_hash
{ a: 1, b: 2, c: 3 }
end
end
class FakeArray
def to_a
[1, 2, 3]
end
class WrappedError
def initialize(error)
@error = error
end
def exception
@error.exception
end
end
puts "Ruby #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}:\n"
require 'benchmark/ips'
def without_block(x,y,z); end
def with_block(x,y,z,&b); end
puts
puts '* no passing a block'
puts
# frozen_string_literal: true
puts "Ruby #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}:\n"
require 'benchmark/ips'
puts
puts '* #to_s:'
puts
a = { 'a' => :ok }
p a.keys.first.frozen? # always true
b = { 'a' => :ok }
p a.keys.first.object_id
p b.keys.first.object_id
p a.keys.first.object_id == b.keys.first.object_id # false for < 2.1, true for >= 2.1
k = [1]
h = {}
h[k] = 1
k << 2
h[k] = 2
p h # {[1, 2]=>1, [1, 2]=>2}
k = [1]
h = {k => 1, (k << 2) => 2}
h = { [] => :ok }
p h[[]] # ok
h = { [:lol] => :ok }
p h[[:lol]] # ok
h = { [] => :ok }
h.first.first << :lol
p h[[:lol]] # nil
@marshall-lee
marshall-lee / 9gag-gif
Last active August 29, 2015 14:20
Download GIFs from 9gag
#!/usr/bin/env ruby
require 'open-uri'
url = ARGV[0]
$stderr.puts 'URL not specified' and exit 1 unless url
uri = URI.parse url
$stderr.puts 'This should be 9gag.com URL' and exit 1 if uri.host != '9gag.com'
m = uri.path.match %r{^/gag/(\w+)$}
$stderr.puts 'Malformed URL!' and exit 1 unless m
@marshall-lee
marshall-lee / online_change_table.rb
Last active November 15, 2018 11:15
Online (non-blocking) ActiveRecord migrations using pt-online-schema-change utility from Percona Toolkit
# Online (non-blocking) ActiveRecord migrations using pt-online-schema-change utility from Percona Toolkit.
#
# In your Rails app put it as lib/online_change_table.rb
# Now you can use it in your migration file:
#
# require 'online_change_table'
#
# class AddSomethingToSomethings < ActiveRecord::Migration
# def up
# online_change_table :somethings do |t|