Skip to content

Instantly share code, notes, and snippets.

num_array = [0,0,0,0,0,0,0,0,0,0]
#populate array
100.times do
random_num = rand(100) + 1
correct_index = (random_num == 100) ? 9 : random_num/10
num_array[correct_index] += 1
end
#print histrogram
@kblake
kblake / sinatra_starter.rb
Created May 4, 2010 00:10
sinatra starter file
require 'rubygems'
require 'sinatra'
require "sinatra/reloader" if development?
require 'dm-core'
DataMapper.setup( :default, "sqlite3://#{Dir.pwd}/my_app.db" )
class Post
include DataMapper::Resource
class Book
include AttributesSort
attr_accessor :author, :date
def initialize(author, date)
@author = author
@date = date
end
end
class Book
include AttributesSort
...
end
def sort_libraries(libraries, key)
libraries.map do |library|
library.attr_sort(:sort_by => key)
end
libraries
module AttributesSort
def self.included(receiver)
receiver.instance_eval do
def do_attributes_sort(collection, attributes)
collection.sort_by { |object| attributes.map { |attribute| object.send(attribute) }}
end
end
Array.class_eval do
def sortable_attributes?
self.all? {|object| @attributes.all?{|attribute| object.respond_to?(attribute)}}
module AttributesSort
def self.included(receiver)
receiver.instance_eval do
def build_attributes(attributes)
"[" << attributes.map!{|attribute| "object.#{attribute}"}.join(",") << "]"
end
def do_attributes_sort(collection, attributes)
attribute_array_string = build_attributes(attributes)
collection.sort_by{|object| eval(attribute_array_string)}
module AttributesSort
def self.included(receiver)
receiver.instance_eval do
def build_attributes(attributes)
"[" << attributes.map!{|attribute| "object.#{attribute}"}.join(",") << "]"
end
def do_attributes_sort(collection, attributes)
attribute_array_string = build_attributes(attributes)
collection.sort_by{|object| eval(attribute_array_string)}
module AttributesSort
def self.included(receiver)
receiver.instance_eval do
def build_attributes(attributes)
"[" + attributes.map!{|a| "object.#{a}"}.join(",") + "]"
end
def do_attributes_sort(collection, options={})
attributes = build_attributes(options[:sort_by])
collection.sort_by{|object| eval(attributes)}
module AttributesSort
def self.included(receiver)
receiver.instance_eval do
def build_attributes(attributes)
"[" + attributes.map!{|a| "object.#{a}"}.join(",") + "]"
end
def do_attributes_sort(collection, options={})
attributes = build_attributes(options[:sort_by])
collection.sort_by{|object| eval(attributes)}
require 'image_size' #imagesize.rubyforge.net
=begin
This is a small test script that I wrote to play around with Ruby's directory
and file handling.
The scenario is this... I have a directory structure as follows:
/rename.rb