Skip to content

Instantly share code, notes, and snippets.

View jodosha's full-sized avatar

Luca Guidi jodosha

View GitHub Profile
if Config::CONFIG['host'] =~ /mswin/i
def socket_active?
return false if not @sock or @sock.closed?
begin
@sock.write("\0")
Timeout.timeout(0.1){ @sock.read } # TODO make 0.1 configurable
rescue Exception
false
end
end
# RubyRedis is an alternative implementation of Ruby client library written
# by Salvatore Sanfilippo.
#
# The aim of this library is to create an alternative client library that is
# much simpler and does not implement every command explicitly but uses
# method_missing instead.
require 'socket'
class RedisClient
class Contact < ActiveRecord::Base
belongs_to :customer
belongs_to :project
end
require 'benchmark'
OK = "OK".freeze
Ok = "OK".freeze
Benchmark.bm(30) do |b|
b.report('upcase constant') do
5_000_000.times { 'OK' == OK }
end
class TagsController < ApplicationController
def show
@tag = Tag.find_by_permalink!(params[:id])
end
end
# Refactoring proposal for ActiveSupport bytes calculations
require "benchmark"
ONE = 1
module Bytes
def bytes
self
end
alias :byte :bytes
require "benchmark"
CONDITION = false
ONE = 1
Benchmark.bm(30) do |b|
b.report("if not true") do
counter = 0
5_000_000.times { counter += ONE if !CONDITION }
end
require "benchmark"
require "rubygems"
require "activesupport"
module Enumerable
def else(&block)
self.respond_to?('empty?') && self.empty? ? yield : self
end
def else_try(&block)
require "benchmark"
TIMES = 5_000_000
HASH = { "foo" => "1000", "bar" => "2000" }
Benchmark.bm(30) do |b|
b.report("old implementation") do
TIMES.times do |i|
keys = %w(foo bar)
result = HASH.inject({}) do |hash, value|
class User
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :subscriptions
has n, :feeds, :through => :subscriptions, :mutable => true
end
class Subscription