Skip to content

Instantly share code, notes, and snippets.

View JoshuaFields's full-sized avatar

Josh Fields JoshuaFields

  • Bindable
View GitHub Profile
@JoshuaFields
JoshuaFields / date_range_sort.php
Created September 24, 2018 12:57
Sort and return date ranges
function getDateRanges($dates) {
$dates = array_unique($dates);
sort($dates);
$count = 0;
$ranges = [];
while ($count < count($dates)) {
$consecutive = true;
@JoshuaFields
JoshuaFields / paginate.blade.php
Last active January 27, 2016 22:01
Paginate a non-eloquent array in Laravel
// Enter the amount of items per page
$perPage = 20;
// Grab the current page (No hidden form required! This just works.)
$currentPage = Input::get('page') - 1;
// Slice your array
$pagedData = array_slice($yourArray, $currentPage * $perPage, $perPage);
// Paginate!
$paginatedArray = Paginator::make($pagedData, count($yourArray), $perPage);
@JoshuaFields
JoshuaFields / favorite-movie-compound.rb
Created February 9, 2015 21:28
favorite-movie-compound
favorite_movies = [
{ title: 'The Big Lebowski', year_released: 1998, director: 'Joel Coen', imdb_rating: 8.2 },
{ title: 'The Shining', year_released: 1980, director: 'Stanley Kubrick', imdb_rating: 8.5 },
{ title: 'Troll 2', year_released: 1990, directory: 'Claudio Fragasso', imdb_rating: 2.5 }
]
favorite_movies.each do |movie|
puts "#{movie[:year_released]}: #{movie[:title]}"
end
@JoshuaFields
JoshuaFields / old_school_roman.rb
Created December 31, 2014 19:01
Old-school Roman numerals. This is from the exercise at the end of chapter 9 in Chris Pine's Learn to Program.
def old_roman_numeral number
i = 0
v = 0
x = 0
l = 0
c = 0
d = 0
m = 0
@JoshuaFields
JoshuaFields / improved_ask.rb
Created December 31, 2014 17:28
The "Improved ask method" example from chapter 9 in Chris Pine's book. I took out the "answer" variable and instead used return.
def ask question
while true
puts question
reply = gets.chomp.downcase
if (reply == "yes" || reply == "no")
if reply == "yes"
return true
#answer = true
else
@JoshuaFields
JoshuaFields / contents_array.rb
Created December 29, 2014 20:17
Table of contents: Array-style!
line_width = 40
contents = ["Table of Contents", "Chapter 1: Getting Started", "page 1 ", "Chapter 2: Numbers", "page 9 ", "Chapter 3: Letters", "page 13"]
puts contents[0].center(line_width)
puts nil
puts contents[1].ljust(line_width) + contents[2].rjust(line_width/3)
puts contents[3].ljust(line_width) + contents[4].rjust(line_width/3)
puts contents[5].ljust(line_width) + contents[6].rjust(line_width/3)
@JoshuaFields
JoshuaFields / word_array.rb
Created December 29, 2014 19:11
Fun with arrays! Enter as many words as you want, and this program will sort them alphabetically—AND capitalize them at no extra cost!
puts "Enter as many words as you like."
puts "I\'ll sort them alphabetically for you!"
puts "Hit \"Enter\" on an empty line when you\'re done."
words = gets.chomp
words_array = []
while words != ""
words_array << words.to_s.capitalize
words = gets.chomp
@JoshuaFields
JoshuaFields / leapyears.rb
Created December 27, 2014 17:48
Leap year calculator
puts "What year would you like to start with?"
year1=gets.chomp
puts "And what year would you like to end with?"
year2=gets.chomp
puts nil
year1.to_i.step(year2.to_i,4) do |num|
if (num % 4 == 0 && !(num % 100 == 0 && num % 400 != 0))
@JoshuaFields
JoshuaFields / 99bottles.rb
Created December 27, 2014 16:17
The basic 99 bottles of beer exercise in chapter 7 of Chris Pine's book
beer = 99
while beer != 0
#to be grammatically correct, I had to add multiple conditions
if beer > 2
beer = beer - 1
puts (beer + 1).to_s + " bottles of beer on the wall, " + (beer + 1).to_s + " bottles of beer!"
puts "Take one down, pass it around, " + beer.to_s + " bottles of beer on the wall!"
puts nil
@JoshuaFields
JoshuaFields / table_of_contents.rb
Created December 27, 2014 03:34
An exercise in chapter 6 of Chris Pine's book that makes use of the center and justify methods
line_width = 50
puts "Table of Contents".center(line_width)
puts nil
puts "Chapter 1: Getting Started".ljust(line_width/2) + "page 1 ".rjust(line_width/2)
puts "Chapter 2: Numbers".ljust(line_width/2) + "page 9 ".rjust(line_width/2)
puts "Chapter 3: Letters".ljust(line_width/2) + "page 13".rjust(line_width/2)