Skip to content

Instantly share code, notes, and snippets.

View mecampbellsoup's full-sized avatar

Matt Campbell mecampbellsoup

View GitHub Profile
@mecampbellsoup
mecampbellsoup / get_subreddits.rb
Created July 31, 2013 16:32
surprising result - was hoping the with_index would allow me give each subreddit an :id without persisting to database
require 'rest_client'
require 'pry'
require 'json'
def get_subreddits
parsed_response = JSON.load(RestClient.get("http://reddit.com/reddits.json"))
parsed_response["data"]["children"].each_with_index do |subreddit,i|
{ subreddit: subreddit["data"]["url"], image: subreddit["data"]["header_img"], name: subreddit["data"]["display_name"], id: i }
end
end
@mecampbellsoup
mecampbellsoup / ranges_overlap_sum.rb
Last active December 20, 2015 11:39
better way to effectively iterate one array over another: http://www.ruby-doc.org/core-2.0/Array.html#method-i-26
def sum_of_intersection?(x1, x2, y1, y2)
intersection = (x1..x2).to_a & (y1..y2).to_a
intersection.inject{|sum,i| sum + i}
end
p sum_of_intersection?(5,15,10,20)
@mecampbellsoup
mecampbellsoup / comments_controller.rb
Last active December 21, 2015 07:39
Fixing comments controller... I think since line 7 comes after the 'create' in line 6, the user_id isn't being set. EDIT: Found a fix, see below.
class CommentsController < ApplicationController
def create
if user_signed_in?
@event = Event.find(params[:event_id])
@comment = @event.comments.build comment_params
binding.pry
if @comment.save
redirect_to @comment.event
else
render :action => 'events/show'
@mecampbellsoup
mecampbellsoup / Gemfile.rb
Last active December 21, 2015 19:59
Excerpts from my Duke Recruits app. Things work great on localhost, but falls apart on Heroku.
source 'https://rubygems.org'
ruby '2.0.0'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
# Use Uglifier as compressor for JavaScript assets
@mecampbellsoup
mecampbellsoup / school_class.rb
Created September 20, 2013 19:34
Tricky initialize...
require 'pry'
class School
def initialize(name, location, ranking, students, instructors)
@name=self[:name]=name
@location=self[:location]=location
@ranking=self[:ranking]=ranking
@students=self[:students]=students
@instructors=self[:instructors]=instructors
@mecampbellsoup
mecampbellsoup / args_var_assignment.rb
Last active December 23, 2015 13:39
Assign args variable in the initialize method definition?
def initialize args=[name_key_value,location_key_value,ranking_key_value,students_array,instructors_array]
args.each do |k,v|
instance_variable_set("@#{k}", v) unless v.nil?
end
end
@mecampbellsoup
mecampbellsoup / alien_pb&j.txt
Created September 23, 2013 22:56
Alienz PB+J.
1. Using your hands, pick up the plastic bag containing the loaf of bread.
2. Set the bag down with the twist top facing up.
3. The twist top is secured by a plastic twist tie - begin to turn the twist tie such that it becomes looser.
4. Once the twist tie is fully loosened, remove it from the bag.
5. Now open the bag and remove the 3rd and 4th (from the top) pieces of bread.
6. Set the two pieces of bread down next to one another, with the curved top of each slice facing outwards/away from you in the same direction.
7. Open both the peanut butter & jelly jars by removing their lids; place the lids directly below each jar so you don't confuse them later.
8. Pick up the knife with the pointed tip away from your hand.
9. Insert the knife into the peanut butter jar first; use the knife to remove roughly 1 oz. of peanut butter (roughly the size of a ping pong ball).
10. Move the knife with peanut butter onto the top-left corner of the leftmost piece of bread you laid out previously.
@mecampbellsoup
mecampbellsoup / flatiron003_profile_mattcampbell.md
Created September 26, 2013 03:33
Profile description for class website!
@mecampbellsoup
mecampbellsoup / fizzbuzz.rb
Created September 26, 2013 03:53
Ruby FizzBuzz!
def fizz_buzz num
puts "FizzBuzz" if num % 15 == 0
puts "Fizz" if num % 3 == 0
puts "Buzz" if num % 5 == 0
puts num
end
(1..50).each do |n|
fizz_buzz n
end
@mecampbellsoup
mecampbellsoup / sort_songs_by_passing_artist_name.rb
Last active December 24, 2015 01:09
Pass the method an artist name and a songs array and watch it werk.
require 'awesome_print' # awesome gem for making stuff look pretty
songs = [
"The Magnetic Fields - 69 Love Songs - Parades Go By",
"The Magnetic Fields - Get Lost - Smoke and Mirrors",
"Neutral Milk Hotel - In An Aeroplane Over the Sea - New Shit",
"Neutral Milk Hotel - Boat Over Water - New Song!",
"Neutral Milk Hotel - In An Aeroplane Over the Sea - Holland 1945",
"Neutral Milk Hotel - Boat Over Water - Another New Song",
"The Magnetic Fields - Get Lost - You, Me, and the Moon",