Skip to content

Instantly share code, notes, and snippets.

// 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',
<div id="multiple-datasets">
<input class="typeahead" type="text" placeholder="Poets and Philosophers">
</div>
<%= 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 %>
# 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
[ITEM for ITEM in RANGE if CONDITION]
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]
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]
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]
class PaginatingDecorator < Draper::CollectionDecorator
delegate :current_page, :total_pages, :limit_value
end
class AnimalsController < ApplicationController
inherit_resources
..
protected
def build_resource_params
animal_params = params.fetch(:animal, {}).permit(:name, :age, :family)
[prepare_animal_params(animal_params)]