Skip to content

Instantly share code, notes, and snippets.

View animals_controller.rb
class AnimalsController < ApplicationController
inherit_resources
..
protected
def build_resource_params
animal_params = params.fetch(:animal, {}).permit(:name, :age, :family)
[prepare_animal_params(animal_params)]
View paginating_decorator.rb
class PaginatingDecorator < Draper::CollectionDecorator
delegate :current_page, :total_pages, :limit_value
end
View select_primes.rb
n = 100000
primes = (0..n).select{ |i| i if (2..i-1).select{ |j| j if i%j == 0}.count == 0}
puts primes.inspect # => [1, 2, 3, 5, 7, ..., 9973]
View primes.rb
n = 100
primes = (1..n).map{ |i| i if (2..i-1).map{ |j| j if i%j == 0}.compact.count == 0}.compact
puts primes.inspect # => [1, 2, 3, 5, 7, ..., 97]
View primes.py
n = 100
whole_factors = [i for i in range(1, n) if 0 == len([j for j in range(2, i-1) if i%j == 0])]
print whole_factors # => [1, 2, 3, 5, 7, ..., 97]
View list_comprehension.py
# collect the even numbers in a given range
# 1. using regular py
evens = []
for i in range(1, 10):
if i % 2 == 0:
evens.append(i)
# 2. using list comprehension
View has_one
<%= semantic_nested_form_for [:admin, @user], :html => {:multipart => true} do |f| %>
<%= f.inputs 'User Information' do %>
<%= f.input :name %>
<%= f.input :age %>
<% end %>
<%= f.inputs 'User's Pet' for: [:pet, f.object.pet || Pet.new] do |pet| %>
<%= pet.input :name %>
<% end %>
View gist:7b1445933077162d1c54
<div id="multiple-datasets">
<input class="typeahead" type="text" placeholder="Poets and Philosophers">
</div>
View gist:153978788aa4d70ac5b0
// In our DOM ready
var poets = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit:10,
prefetch: {
// url points to a json file that contains an array of author names
// e.g. ['Baudelaire', 'Beckett']
url: './poets.json',