This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Book | |
include AttributesSort | |
attr_accessor :author, :date | |
def initialize(author, date) | |
@author = author | |
@date = date | |
end | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Book | |
include AttributesSort | |
... | |
end | |
def sort_libraries(libraries, key) | |
libraries.map do |library| | |
library.attr_sort(:sort_by => key) | |
end | |
libraries |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |