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!{|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 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
| 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
| 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
| 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
| 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 'twitter' | |
| require 'highline/import' | |
| username = ask("Please enter twitter name") | |
| unless username.empty? | |
| size = 50 #limit to 50 followers arbitrarily |
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 'pivotal-tracker' | |
| require 'highline/import' | |
| HighLine.track_eof = false | |
| def story_filter(chosen_state) | |
| lambda {|story| story.owned_by == @name && story.current_state == chosen_state} | |
| end |