Skip to content

Instantly share code, notes, and snippets.

@benolee
benolee / Gemfile
Created June 1, 2012 05:23
routing for nested resources
source :rubygems
gem 'actionpack', '3.2.3'
gem 'activerecord', '3.2.3'
gem 'activesupport', '3.2.3'
gem 'railties', '3.2.3'
gem 'tzinfo'
gem 'sqlite3'
gem 'thin'
@benolee
benolee / session
Created June 8, 2012 03:43
pry session with steve's blog
[1] pry(main)> user = User.new
=> #<User @id=nil @email=nil @encrypted_password=nil>
[2] pry(main)> User.all
=> [#<User @id=1 @email="[email protected]" @encrypted_password="$2a$05$7N1xabSr/SPFJuCKpm62nurvRgsF59GfKu3ZPnK9H9nlnkfytAC62">, #<User @id=2 @email="[email protected]" @encrypted_password="$2a$05$TkRn2veaV/uBEQp0912Vweb2Kf1dnX2DpyM5fk.8SgW7C/ufpwlK6">]
[3] pry(main)> user.email = '[email protected]'
=> "[email protected]"
[4] pry(main)> user.password = 'secretpass'
=> "secretpass"
[5] pry(main)> user.save
=> true
@benolee
benolee / Gemfile
Created August 25, 2012 02:47
a naive implementation of dataflow variables in ruby
source :rubygems
gem 'minitest'
@benolee
benolee / avoiding-hash-lookups-in-ruby.md
Created September 5, 2012 14:20
Avoiding Hash Lookups in a Ruby Implementation

Permalink

Avoiding Hash Lookups in a Ruby Implementation

I had an interesting realization tonight: I'm terrified of hash tables. Specifically, my work on JRuby (and even more directly, my work optimizing JRuby) has made me terrified to ever consider using a hash table in the hot path of any program or piece of code if there's any possibility of eliminating it. And what I've learned over the years is that the vast majority of execution-related (as opposed to data-related, purely dynamic-sourced lookup tables) hash tables are totally unnecessary. Some background might be interesting here.

Hashes are a Language Designer's First Tool

@benolee
benolee / events.rb
Created September 8, 2012 14:37
Events
require 'bundler/setup'
Bundler.require :default
require 'securerandom'
require 'active_support/notifications'
require 'active_support/concern'
module Events
include ActiveSupport::Concern
@benolee
benolee / dummy.css
Created October 17, 2012 23:49
dummy
.out {
color: red;
font-family: 'Inconsolata', sans-serif;
}
@benolee
benolee / jruby-1.7.0-startup-time.md
Created October 23, 2012 14:53
jruby 1.7.0 startup time with client mode
$ jruby -v
jruby 1.7.0 (1.9.3p203) 2012-10-22 ff1ebbe on Java HotSpot(TM) 64-Bit Server VM 1.6.0_33-b03-424-10M3720 [darwin-x86_64]

$ time jruby -e 'require "rubygems"; require "active_support"'
real	0m1.426s
user	0m2.681s
sys	0m0.180s

$ export JAVA_OPTS='-d32'  # switch to 32-bit client mode

$ jruby -v

@benolee
benolee / 00-creating-a-ruby-extension.md
Last active August 6, 2017 17:18
how easy it is to create and load a C extension in ruby

create a new dir

$ mkdir /tmp/ruby
$ cd /tmp/ruby

create extconf.rb

@benolee
benolee / 00-secret_ivar.md
Last active December 11, 2015 00:19
get and set a secret instance variable

first, make the Makefile

$ ruby extconf.rb
creating Makefile

next, build the extension

@benolee
benolee / irb_history.rb
Created January 14, 2013 20:11
playing with Fiddle::Function
>> require 'fiddle'
=> true
>> func = Fiddle::Function.new(DL::Handle::DEFAULT['rb_eval_string'], [DL::TYPE_VOIDP], DL::TYPE_VOIDP)
=> #<Fiddle::Function:0x000001010404c8>
>> func.call("1 + 1")
=> #<DL::CPtr:0x00000101821930 ptr=0x00000000000005 size=0 free=0x00000000000000>
>> func.call("1 + 1").to_value
=> 2
>> func.call("puts 'hello'").to_value
hello