Skip to content

Instantly share code, notes, and snippets.

View dylanerichards's full-sized avatar

Dylan Richards dylanerichards

View GitHub Profile
@dylanerichards
dylanerichards / guide.rb
Created January 4, 2014 04:50
How do I print out a list of users in order of score from greatest to least?
class Guide < ActiveRecord::Base
belongs_to :user
acts_as_votable
def score
upvotes.count - downvotes.count
end
end
Started POST "/guides/2-sdkfjsk/comments" for 127.0.0.1 at 2014-01-08 09:14:59 -0500
Processing by CommentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"D2BvBIgU+tniZvr2NgQE/TpHY6J2xHOUm701jqTcJ9A=", "comment"=>{"body"=>"", "user_id"=>"some value"}, "{:id=>\"comments_guide_id\", :name=>\"comments"=>{"guide_id"=>{"\", :value=>2}"=>""}}, "commit"=>"Create Comment", "guide_id"=>"2-sdkfjsk"}
Completed 500 Internal Server Error in 1ms
NoMethodError (undefined method `[]=' for nil:NilClass):
app/controllers/comments_controller.rb:27:in `create'
<p id="notice"><%= notice %></p>
<h1>Now Discussing: <%= @guide.title %></h1>
<p>
<strong>Link to Guide:</strong>
<%= link_to @guide.title, "http://#{@guide.link}" %>
</p>
<p>
@dylanerichards
dylanerichards / guides_controller.rb
Created January 9, 2014 23:42
Acts as votable. Trying to make it so that users don't have to sign in to upvote/downvote.
def upvote
@guide = Guide.find(params[:id])
@guide.liked_by current_user
redirect_to :back
end
def downvote
@guide = Guide.find(params[:id])
@guide.downvote_from current_user
redirect_to :back
@dylanerichards
dylanerichards / cipher.rb
Created May 14, 2014 08:20
How do you feel?
def north_korean_cipher(coded_message)
input = coded_message.split('') # splits the coded message into array of letters
letters = %w(a b c d e f g h i j k l m n o p q r s t u v w x y z)
code = letters.rotate(-4)
@dylanerichards
dylanerichards / javascript_solo_challenge.js
Created May 17, 2014 09:13
javascript_solo_challenge.js
// This is a Solo Challenge.
// There is a section below where you will write your code.
// Do not alter this object here.
var terah = {
name: "Terah",
age: 32,
height: 66,
@dylanerichards
dylanerichards / pseudoku.txt
Last active August 29, 2015 14:03
Sudoku 1 pseudocode
☐ create variable containing options [1-9]
☐ look at first row and see what numbers from 1-9 are missing
☐ then look at column and see what else is missing
☐ finally, check the box
☐ (note that we're removing values from 'options' every time we check)
☐ compare what's missing to our options
☐ when options.size == 1, then we know that that's the answer
☐ if not, then move on to next empty cell
OPTIONS = [1,2,3,4,5,6,7,8,9]
@dylanerichards
dylanerichards / fraction_to_num.rb
Created June 27, 2014 22:11
fraction to numbers challenge
input1 = "100/2"
input2 = '100/90'
operator = '-'
def to_num(frac)
components = frac.split('/').map(&:to_i)
value = (components[0].to_f / components[1].to_f)
value
end
def pig_latin(word)
if %w(a e i o u).include? word[0]
word + "ay"
else
word[1..-1] + word[0] + "ay"
end
end
@dylanerichards
dylanerichards / final.rb
Created July 5, 2014 10:52
Midpoint calculator: a representation of how far I've come. The first example, written some time in late 2013, showcases a functional solution. The second, more recent, example demonstrates an object-oriented solution.
class MidpointCalculator
def initialize(information)
@quantity1 = information[:quantity1].to_f
@quantity2 = information[:quantity2].to_f
@price1 = information[:price1].to_f
@price2 = information[:price2].to_f
end
def calculate
numerator = ((@quantity1 - @quantity2) / (@quantity1 + @quantity2 ) / 2)