This file contains 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
Benchmark.ips do |x| | |
x.report { x="str"; x =~ /foo/ || x =~ /bar/ } | |
x.report { x="str"; x.include?("foo") || x.include?("bar") } | |
x.report { x="str"; x["foo"] || x["bar"] } | |
end |
This file contains 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 Symbol | |
def [](*args) | |
proc do |object| | |
object.send self, *args | |
end | |
end | |
def ~ | |
proc do |object, *args| | |
object.send self, *args |
This file contains 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" | |
require "benchmark/ips" | |
range = Array(1..100_000) | |
Benchmark.ips do |x| | |
x.report("each") { range.each { |x| } } | |
x.report("for") { for x in range; end } | |
end |
This file contains 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" | |
require "benchmark/ips" | |
require "dish" | |
hash = { | |
title: "My Title", | |
authors: [ | |
{ id: 1, name: "Mike Anderson" }, | |
{ id: 2, name: "Well D." } | |
], |
This file contains 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 'function' | |
# A fibonacci function. | |
fib = Function.new | |
fib[0] = 0 | |
fib[1] = 1 | |
fib[Integer] = ->(i) { fib[i - 2] + fib[i - 1] } | |
p fib[0] # => 0 |
This file contains 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 "pattern_matching" | |
# Let's abuse case statements and the case equality method (#===) to implement | |
# pattern matching in Ruby. | |
# | |
# Note that this'll only work in Ruby 2.1.0-dev (2.0.0-p247 and lower does not | |
# honour refined #=== in case statements). | |
using PatternMatching |
This file contains 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" | |
require "benchmark/ips" | |
array = Range.new(1,1_000_000).to_a | |
Benchmark.bmbm do |x| | |
x.report("#inject &:*") { array.inject &:* } | |
x.report("#inject :*") { array.inject :* } | |
end |
This file contains 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 Mixin | |
def foo | |
puts "foo" | |
end | |
end | |
class A; end | |
a = A.new |
This file contains 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
Fib = Hash.new { |hash, key| hash[key] = hash[key - 2] + hash[key - 1] } | |
Fib.merge! 0 => 0, 1 => 1 | |
require 'minitest' | |
require 'minitest/autorun' | |
describe Fib do | |
it 'fibs' do | |
assert_equal Fib[0], 0 | |
assert_equal Fib[1], 1 |
This file contains 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
object = [] | |
object.object_id # => 70185911630120 | |
hash = Hash.new([]) | |
hash[:one].object_id # => 70185911630120 | |
hash[:two].object_id # => 70185911630120 |