Skip to content

Instantly share code, notes, and snippets.

require 'benchmark'
array = (0..10000000).to_a
benchmark = Benchmark.bm(10) do |x|
x.report("Ruby 2.0: ") do
100000.times { array.bsearch { |e| 7777777 <=> e } }
end
require 'bsearch'
x.report("bsearch gem:") do
@gkop
gkop / notes.md
Created January 9, 2013 05:09
Noisebridge Ruby and Rails class 1/8/13: Rails 4 New Features (notes)
@gkop
gkop / diff of Gemfile.lock files
Created December 12, 2012 18:36
Comparison of Rails 3.2.9 bare app vs. Rails-API 3.2.9 bare app
gabe2@moth:~/play $ diff rails-standard-app/Gemfile.lock rails-api-app/Gemfile.lock
33,39d32
< coffee-rails (3.2.2)
< coffee-script (>= 2.2.0)
< railties (~> 3.2.0)
< coffee-script (2.2.0)
< coffee-script-source
< execjs
< coffee-script-source (1.4.0)
41,42d33
@gkop
gkop / Gemfile
Created October 31, 2012 02:40
10/30 Rails class
source 'https://rubygems.org'
gem 'rails', '3.2.3'
# authentication
gem 'devise'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
@gkop
gkop / production.rb
Created October 24, 2012 19:23
Best practice for methods used only during pre-initialization?
# This simple solution permanently adds a method on Object that
# will never be used outside this file.
def compile_asset?(path)
# ... lots of complicated logic ...
# returns boolean
end
Foo::Application.configure do
...
@gkop
gkop / gist:3921201
Created October 19, 2012 23:03
Ruby's "and" and "or" operators explained

|| and && bind with the precedence that you expect from boolean operators in programming languages (&& is very strong, || is slightly less strong).

and and or have lower precedence.

For example, unlike ||, or has lower precedence than =:

> a = false || true
 => true 
> a

=> true

@gkop
gkop / gist:3824607
Created October 3, 2012 02:34
Gemfile for etherbot
source "https://rubygems.org"
# these are the Ruby bindings for the etherpad-lite API
gem "etherpad-lite"
# leverages unix diff to discover changes to pad content
gem "diff-lcs"
# boilerplate debugging tool
gem "pry"
@gkop
gkop / etherbot.rb
Created October 3, 2012 02:51
Etherbot script
require 'etherpad-lite'
require 'piggy_latin'
require 'pry'
require 'pry-nav'
ether = EtherpadLite.connect('http://pad.coshx.com', 'KHoaXT0XuEH2lkXkeAmYry4W3k1emR15')
pad = ether.pad('noisebridge')
i = 0
@gkop
gkop / gist:3747504
Created September 19, 2012 03:29
Chat client
$(document).ready ->
ws = new WebSocket("ws://localhost:9000/chat")
ws.onopen = ->
console.log "connected..."
ws.onmessage = (event) ->
console.log event
$("<li>#{event.data}</li>").prependTo("#msg")
@gkop
gkop / gist:3747289
Created September 19, 2012 02:25
Chat server
require 'bundler/setup'
Bundler.require
require 'goliath/websocket'
class Chat < Goliath::WebSocket
include Goliath::Rack::Templates
def on_open(env)
env.logger.info("CHAT OPEN")