Skip to content

Instantly share code, notes, and snippets.

View masonfmatthews's full-sized avatar

Mason F. Matthews masonfmatthews

  • Hillsborough, NC
View GitHub Profile
class Shirt
def initialize(c)
@color = c
end
def number_of_arms
2
end
def color
@masonfmatthews
masonfmatthews / battleship.md
Created January 12, 2015 19:22
Battleship Test

Battleship Instructions

Description

Write detailed instructions on how to play a game of Battleship. Compose these instructions as if they were written for a computer interacting with a human.

Objectives

Learning Objectives

module ItemsHelper
def display_array_contents(items)
result = "<table>"
items.each do |i|
result << "<tr>"
result << "<td>#{i.name}</td>"
result << "<td>#{i.price}</td>"
result << "</tr>"
end
class PhotosController < ApplicationController
before_action :set_photo, only: [:show, :edit, :update, :destroy, :edit_tags, :update_tags]
# GET /photos
# GET /photos.json
def index
@photos = Photo.all
end
# GET
<h1>Edit Tags for <%= @photo.name %></h1>
<%= form_tag update_tags_photo_path do %>
<ul>
<% @tags.each do |t| %>
<li>
<%= check_box_tag "tags[#{t.id}]", true, @photo.tags.include?(t) %>
<%= t.name %>
</li>
<% end %>
Rails.application.routes.draw do
resources :photos do
member do
get 'edit_tags'
post 'edit_tags'
post 'update_tags'
end
collection do
get 'dashboard'
end
def first_name(name)
return "" unless name
array = name.split
if array.count < 2
""
else
array[0..-2].join(" ")
end
end
@masonfmatthews
masonfmatthews / gist:91e46d2eafb7a2c23062
Created March 3, 2015 15:39
Time Passed Methods (requires Rails to run)
def time_passed(updated_at)
difference = (Time.now - updated_at.to_time)
time = (difference / 1.day).round
units = "day"
if time == 0
time = (difference / 1.hour).round
units = "hour"
if time == 0
time = (difference / 1.minute).round
units = "minute"
@masonfmatthews
masonfmatthews / gist:66520677a19215a02245
Created May 8, 2015 20:00
More compact hash creation
# Optimal Moves Data
hard = {8 => {},9 => {}, 10 => {}, 11 => "Double if possible, otherwise Hit",
12 => {}, 13 => {}, 14 => {}, 15 => {}, 16 => {},
}
(5..7).each {|n| hard[n] = Hash.new("Hit")}
((2..4).to_a + (7..11).to_a).each {|n| hard[8][n] = "Hit"}
(5..6).each {|n| hard[8][n] = "Double if possible, otherwise Hit"}
(2..6).each {|n| hard[9][n] = "Double if possible, otherwise Hit"}
(7..11).each {|n| hard[9][n] = "Hit"}