This file contains 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
defmodule Chop do | |
def guess(actual, range = low..high) when actual >= low and actual <= high do | |
_guess(actual, ask(div(low + high, 2)), range) | |
end | |
defp _guess(actual, actual, _) do | |
IO.puts "\nYES! It is #{actual}\n" | |
end |
This file contains 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
# Set prefix to Ctrl-a | |
set -g prefix C-a | |
bind C-a send-prefix | |
# Unbind previous Ctrl-b prefix | |
unbind C-b | |
# Change default delay between keystrokes | |
set -s escape-time 1 |
This file contains 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
# Compiled source # | |
################### | |
*.com | |
*.class | |
*.dll | |
*.exe | |
*.o | |
*.so | |
# Packages # |
This file contains 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 FullUrlValidator < ActiveModel::EachValidator | |
VALID_SCHEMES = %w(http https) | |
def validate_each(record, attribute, value) | |
unless valid_full_url?(value) | |
record.errors[attribute] << (options[:message] || 'is not a valid URL') | |
end | |
end | |
This file contains 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
def if_multiple_pages?(collection, per_page = 10) | |
pages = (collection.size / (per_page || 10).to_f).ceil | |
yield pages if pages > 1 | |
end |
This file contains 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 Product < ActiveRecord::Base | |
# ... other code | |
has_and_belongs_to_many :products, | |
:join_table => "related_products", | |
:foreign_key => "product_a_id", | |
:association_foreign_key => "product_b_id", | |
:finder_sql => 'SELECT DISTINCT products.* FROM products INNER JOIN related_products ON (#{id} = related_products.product_a_id OR #{id} = related_products.product_b_id) WHERE (products.id <> #{id})', | |
:insert_sql => 'INSERT INTO related_products (product_a_id, product_b_id) VALUES (#{id}, #{record.id})', | |
:delete_sql => 'DELETE FROM related_products WHERE (product_a_id = #{id} AND product_b_id IN (#{record.id})) OR (product_a_id = #{record.id} AND product_b_id IN (#{id}))', |
This file contains 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 Category < ActiveRecord::Base | |
has_many :categories, :order => 'order_by asc' | |
belongs_to :category | |
scope :order, order("order_by asc") | |
scope :main, where(:category_id => nil) | |
def options_for_select( selected = nil ) | |
selected_attribute = ' selected="selected"' if self == selected | |
options_output = "<option value='#{self.id}'#{selected_attribute}>#{self.title}</option>" |
This file contains 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
# | |
# Credit goes to jamis | |
# http://37signals.com/svn/posts/2742-the-road-to-faster-tests | |
# | |
# wipe almost all of the instance variables from the object | |
teardown :scrub_instance_variables | |
@@reserved_ivars = %w(@loaded_fixtures @test_passed @fixture_cache @method_name @_assertion_wrapped @_result) |
This file contains 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
Factory.define :album do |album| | |
album.name 'Album Name' | |
album.description 'Album Description' | |
album.active true | |
album.association :cover_image, :factory => :image | |
end |
OlderNewer