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
#!/usr/bin/env ruby | |
require './dancer' | |
require './gangnam_style' | |
dancer = Dancer.new('PSY', GangnamStyle.new) | |
dancer.dance! |
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
#!/usr/bin/env ruby | |
require './injection' | |
require 'pry' | |
class CallTracker < BasicObject | |
attr_reader :tracked_calls | |
def initialize | |
@tracked_calls = ::Hash.new { |h, k| h[k] = 0 } |
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
#!/usr/bin/env ruby | |
# http://www.ruby-doc.org/core-1.8.7/classes/Object.html#M000006 : | |
# | |
# obj.instance_exec(arg...) {|var...| block } => obj | |
# | |
# Executes the given block within the context of the receiver (obj). | |
# In order to set the context, the variable self is set to obj while | |
# the code is executing, giving the code access to obj‘s instance variables. | |
# Arguments are passed as block parameters. |
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
#!/usr/bin/env ruby | |
# | |
# $ ./wth_mysql.rb | |
# user system total real | |
# mysql 0.020000 0.010000 0.030000 ( 0.617876) | |
# sqlite 0.000000 0.000000 0.000000 ( 0.001234) | |
# postgresql 0.000000 0.000000 0.000000 ( 0.002423) | |
srand 123456789 | |
require 'active_record' |
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
# If you put a DSL stub object like this inside an array and call array#flatten without | |
# being sure to call super on :to_ary, you get fun stuff like this: | |
# can't convert Squeel::Nodes::Stub to Array | |
# (Squeel::Nodes::Stub#to_ary gives Squeel::Nodes::KeyPath) | |
def method_missing(method_id, *args) | |
super if method_id == :to_ary | |
if args.empty? | |
KeyPath.new(self, method_id) | |
elsif (args.size == 1) && (Class === args[0]) | |
KeyPath.new(self, Join.new(method_id, Arel::InnerJoin, args[0])) |
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
============================================================ | |
Benchmarking with Ruby 1.9.3 and ActiveRecord 3.2.0.beta | |
============================================================ | |
user system total real | |
============================================================ | |
RSS : 31432k (31432k) | |
Objects : 102195 (102195) | |
============================================================ | |
SINGLE-VALUE TEST |
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
==================== | |
3.0.10 | |
==================== | |
user system total real | |
============================================================ | |
RSS : 37592k (37592k) | |
Objects : 182328 (182328) | |
============================================================ | |
all 0.650000 0.010000 0.660000 ( 0.682556) | |
============================================================ |
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
rs = (0..10000).to_a.sample(30) | |
rs.each do |r| | |
case r | |
when :zero?.to_proc then puts "#{r} is zero" | |
when :even?.to_proc then puts "#{r} is even" | |
when :odd?.to_proc then puts "#{r} is odd" | |
else | |
raise 'unpossible' | |
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
module Enumerable | |
def any?(&block) | |
self.each do |v| | |
return true if yield(v) | |
end | |
false | |
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
require 'benchmark' | |
haystack = (1..1_000_000).to_a | |
needles = 1.upto(100).map {|n| n * 10_000} | |
module EachDetector | |
def self.find(haystack, needle) | |
haystack.each do |v| | |
return true if v == needle | |
end |