This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A PORO controller can be just an object that takes the request, response, | |
# and streaming-socket objects into the constructor. It can do the typical | |
# ActionController things, like get the objects it needs and tell the response | |
# to render the view with those objects. | |
class WidgetsController | |
attr_accessor :request, :response, :stream | |
def initialize(request, response, stream_socket) | |
@request = request |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Class | |
# Important: keep 'initialize' at the end of this array | |
COMMON_INIT_MISSPELLINGS = %w(intitialize intialize initialise initialize) # etc... | |
def new(*args) | |
obj = allocate | |
obj.send COMMON_INIT_MISSPELLINGS.find { |m| obj.respond_to? m }, *args | |
obj | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
➜ rails_three time RAILS_ENV=production rake assets:precompile | |
/Users/jamie/.rvm/rubies/ruby-2.0.0-p247/bin/ruby /Users/jamie/.rvm/gems/ruby-2.0.0-p247@global/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets | |
RAILS_ENV=production rake assets:precompile 20.75s user 1.82s system 93% cpu 24.089 total |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative '../lib/perpetuity/attribute_set.rb' | |
require_relative '../lib/perpetuity/attribute.rb' | |
require 'benchmark/ips' | |
include Perpetuity | |
attributes = AttributeSet.new | |
names = %w(foo bar baz quux abc def ghi) | |
names.each do |name| | |
attributes << Attribute.new(name) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ImmutableQueue | |
attr_reader :first, :last, :count | |
def initialize | |
@count = 0 | |
end | |
def << object | |
object = object.dup rescue object | |
node = Node.new(object) | |
duplicate do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person | |
attr_reader :name, :enemies | |
def initialize name | |
@name = name | |
@enemies = [] | |
end | |
def add_enemies enemies | |
@enemies.concat enemies |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
With 115k results: | |
Rehearsal ---------------------------------------------------------------------- | |
sorting Mongo query in Mongo 0.990000 0.100000 1.090000 ( 2.193201) | |
sorting Mongo query in Ruby 0.980000 0.030000 1.010000 ( 1.171781) | |
sorting Postgres query in Postgres 0.100000 0.020000 0.120000 ( 2.143469) | |
sorting Postgres query in Ruby 0.340000 0.030000 0.370000 ( 0.485265) | |
------------------------------------------------------------- total: 2.590000sec | |
user system total real |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'set' | |
class NoNilSet | |
include Enumerable | |
def initialize(values=[]) | |
@set = Set.new | |
@set += Array(values).compact | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
def remove_one values | |
values = values.dup | |
new_array = [] | |
each do |value| | |
if values.include? value | |
values.delete value | |
else | |
new_array << value | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Module | |
def namespace | |
path = name.split('::')[0...-1] | |
path.reduce(Object) { |namespace, const| namespace.const_get(const) } | |
end | |
end |