Skip to content

Instantly share code, notes, and snippets.

@bayendor
bayendor / gist:bd055224d757e64c2663
Last active August 29, 2015 14:10
Storing State in Sessions
  1. What's the difference between a cookie and a session? A session lasts until the browser is closed, a cookie is persistent state stored in a small text file.
  2. What's serialization and how does it come into play with cookies? The cookies stores serialized data about the session.
  3. Can a cookie be shared by more than one user? How/why? Probably.
  4. What would it mean to store a shopping cart in a cookie? The shopping cart would persist from session to session.
  5. What advantages/disadvantages are there between cookie-stored carts and database-stored carts?
@bayendor
bayendor / session_example.rb
Last active August 29, 2015 14:10
Rails Session Use Examples
class Counter
def self.counter(session)
session[:hit_counter] ||= 0
session[:hit_counter] += 1
end
end
# access with
class ItemsController < ApplicationController
before_action :load_counter
require 'rspec'
def sum(numbers)
numbers.inject(0) { |sum, number| sum + number }
end
def product(numbers)
numbers.inject(1) { |sum, number| sum * number }
end
namespace :deploy do
desc "Deploys app to Github & production."
task all: :environment do
if system("rake test:all") == false
puts "The tests fail."
break
end
puts "The tests passed."
@bayendor
bayendor / arctive_record_queries.rb
Last active August 29, 2015 14:15
ActiveRecord Homework for Module 4 of Turing
# Find all the items that have the word "elegant" in it.
items = Item.where("name LIKE ?", "%elegant%")
# Add three orders to a user.
3.times do |i|
user = User.first
order = Order.create!(user_id: user.id)
order.items << Item.find(i + 1)
end
@bayendor
bayendor / Archaeology.md
Last active August 29, 2015 14:18
Software Archaeology Code Screen Exercise

The cryptic Ruby function takes an array as an argument and returns an array of the items that occur more than once in the original array.

It does this using the inject method specifying an empty Hash as the accumulator. It iterates over each item in the array by referencing the item index position, and passes the resulting item into the accumulator. This builds up a hash of key/value pairs with the key being the item from the initial collection and the value representing the number of times the item appears in the array. It then uses the reject method on the resulting hash and iterates over the hash returning a new array containing the items in self for which the given block is false.

Using idiomatic Ruby a cleaner way to write this function would be:

def show_duplicates(collection)
  collection.select { |element| collection.count(element) > 1 }.uniq
end
@bayendor
bayendor / flatten_clone.rb
Last active August 29, 2015 14:19
Flatten an array in ruby without using `flatten`
def flatten_clone(array, level = -1, result = [])
array.each do |element|
if element.is_a?(Array) && level != 0
flatten_clone element, level - 1, result
else
result << element
end
end
result
end
@bayendor
bayendor / docker
Last active October 6, 2015 15:56 — forked from chernjie/logrotate.d-docker
Logrotate docker logs, copy this file to /etc/logrotate.d/docker
/data/docker/containers/*/*-json.log {
daily
rotate 7
size 100M
compress
delaycompress
missingok
}
@bayendor
bayendor / puppet.vim
Created April 4, 2016 14:05
VIM Config settings for working with Puppet
" Set up puppet manifest and spec options
au BufRead,BufNewfile *.pp
\ set filetype=puppet
au BufRead,BufNewfile *_spec.rb
\ nmap <F8 :!rspec --color %<CR>
" Enable indentation matching for =>'s
filetype plugin indent on
" turn off auto adding comments on next line
@bayendor
bayendor / keybase.md
Created April 22, 2016 15:44
keybase.md

Keybase proof

I hereby claim:

  • I am bayendor on github.
  • I am dbayendor (https://keybase.io/dbayendor) on keybase.
  • I have a public key ASB1V69Is1m_YenJTKqCUfMSmusHP5quka8emz0F5BjJNQo

To claim this, I am signing this object: