Skip to content

Instantly share code, notes, and snippets.

View tjsingleton's full-sized avatar

TJ Singleton tjsingleton

View GitHub Profile
@tjsingleton
tjsingleton / benchmarked_block.rb
Created July 8, 2010 04:44
RMU Entrance Exam Multi-VM Benchmarks #2
n.times { doc.add_text(msg) }
n.times { doc.undo }
n.times { doc.redo }
# modules work! :) If we define_method on a module and include it, it works the same.
class A
include World
end
module World
def hello
puts "World"
end
@tjsingleton
tjsingleton / results.txt
Created July 9, 2010 14:58
benchmark showing that symbols live forever
100000 Strings
After Block: 3318
After Forced GC: 31
100000 Symobls
After Block: 1519
After Forced GC: 98619
@tjsingleton
tjsingleton / no_side_effects.rb
Created August 18, 2010 18:19
Example of the difference between a side effect free method and a method with side effects.
class Value
attr_reader :value
def initialize(value)
@value = value
end
def add(i)
class.self.new(i + @value)
end
end
@tjsingleton
tjsingleton / 1_branches_to_graphviz.rb
Created August 18, 2010 21:40
Example: ruby branches_to_graphviz.rb branches1.json > branches1.gv
require "json"
file = ARGV[0]
branches = File.open(file) {|f| JSON.parse(f.read).freeze }
LABEL_STRING = '"%s" [label="%s"]'
GRAPH_STRING = '"%s" -> "%s"'
cells = []
puts "digraph graph_example {"
@tjsingleton
tjsingleton / gist:640068
Created October 22, 2010 06:50
Using define method with an module

We have a widget that we want to extend

class Widget
end

so we open it and define_method

class Widget
  define_method :hello do

puts "Hello!"

@tjsingleton
tjsingleton / seeds.rb
Created October 29, 2010 19:04
Seeds fix to reloads authlogic for rails 2.3.x
modules_to_load = [
Authlogic::ActsAsAuthentic::SingleAccessToken::Methods::InstanceMethods,
Authlogic::ActsAsAuthentic::SingleAccessToken::Methods,
Authlogic::ActsAsAuthentic::SessionMaintenance::Methods,
Authlogic::ActsAsAuthentic::PersistenceToken::Methods::InstanceMethods,
Authlogic::ActsAsAuthentic::PersistenceToken::Methods,
Authlogic::ActsAsAuthentic::PerishableToken::Methods::InstanceMethods,
Authlogic::ActsAsAuthentic::PerishableToken::Methods,
Authlogic::ActsAsAuthentic::Password::Methods::InstanceMethods,
Authlogic::ActsAsAuthentic::Password::Methods,
@tjsingleton
tjsingleton / 99bottles.rb
Created November 5, 2010 14:18
Code golf that prints the lyrics to 99 bottles of beer in 219B.
A=" on the wall"
def z n
"%s bottle#{"s" if n>1} of beer"%n
end
99.downto(1){|n|a,b,c=n,*n>1?[n-1,"Take one down and pass it around"]:[99,"Go to the store and buy some more"]
d=z a
puts"%s, %s.
"*2%[d+A,d,c,z(b)+A]+"
"}
@tjsingleton
tjsingleton / example_of_dynamic_message.txt
Created November 19, 2010 20:46
Example of a dynamic error message.
ruby-1.9.2-p0 > User.new.save!
ActiveRecord::RecordInvalid: Validation failed: Email is too short (minimum is 6 characters), Email should look like an email address., Email is too short (minimum is 3 characters), Email should use only letters, numbers, spaces, and .-_@ please., Password is too short (minimum is 4 characters), Password confirmation is too short (minimum is 4 characters), First name can't be blank, Last name can't be blank, Company name can't be blank, User type is not included in the list, Company size is not included in the list, Industry is not included in the list
@tjsingleton
tjsingleton / custom_error.rb
Created November 19, 2010 19:01
Is there a better way to do this?
class CustomError < StandardError
def initialize(message = nil)
message ||= "Default message."
super(message)
end
end