Skip to content

Instantly share code, notes, and snippets.

@ivanbrennan
ivanbrennan / ruby_phone_format
Last active December 24, 2015 02:09
Ruby Phone Format
# Download this file:
# https://gist.github.com/scottcreynolds/ac1b5c8d96de0c91bf7c/download
# Run it from your terminal with:
# ruby ruby_phone_format.rb
# (Just make sure you are in the right directory)
# ======================================
# Ignore All This Code
# ======================================
@ivanbrennan
ivanbrennan / apple-picker.rb
Created September 30, 2013 22:10
Apple Picker
def apple_picker(array)
apples_etc = array.collect { |element| element if element == "apple"}
apples_etc.compact
end
def apple_picker(array)
array.select {|element| element == "apple"}
end
# select returns an array of the original elements that make block evaluate to true, while
@ivanbrennan
ivanbrennan / hashes.rb
Last active December 24, 2015 08:29
Hashes
movie_collection = { "horror" => ["Kindergarten Cop", "Psycho", "Last House on the Left"],
"comedy" => ["Beverly Hills Cop", "Jackass"],
"fantasy" => ["The Hobbit", "Willow", "The Princess Bride"]}
recipes = { "bolognese sauce" => ["olive oil", "onion", "garlic", "tomato", "basil", "oregano"],
"omlette" => ["eggs", "milk", "cheese"],
"chicken adobo" => ["chicken", "vinegar", "onions", "garlic", "ginger", "soy sauch"]}
user_profiles = { "Jordan" => { "favorite colors" => ["red", "yellow", "blue"],
"personal_essays" => ["essay 1", "essay 2", "essay 3"] },
@ivanbrennan
ivanbrennan / reverse-each-word.rb
Created September 30, 2013 22:07
Reverse each word
def reverse_each_word(sentence)
words = sentence.split
back_words = words.collect { |word| word.reverse }
back_words.join(" ")
end
@ivanbrennan
ivanbrennan / holiday-supplies.rb
Created September 30, 2013 22:11
Holiday Supplies
holiday_supplies = {
:winter => {
:christmas => ["Lights", "Wreath"],
:new_years => ["Party Hats"]
},
:summer => {
:forth_of_july => ["Fireworks", "BBQ"]
},
:fall => {
:thanksgiving => ["Turkey"]
@ivanbrennan
ivanbrennan / deli-counter.rb
Created September 30, 2013 22:26
Deli Counter
def take_a_number(deli, person)
deli << person
deli.length
end
def now_serving(deli)
puts "Currently serving #{deli.first}"
deli.shift
end
@ivanbrennan
ivanbrennan / hashketball.rb
Created October 1, 2013 03:57
Hashketball
game = {
:home => {
:name => "Wu-Tang Clan",
:colors => ["Black", "Yellow"],
:players => [
{ :name => "Method Man",
:number => 6,
:shoe_size => 10,
:stats => {
:points => 49,
@ivanbrennan
ivanbrennan / version-sort.rb
Last active December 24, 2015 10:09
Version Sort
class Array
def version_sort
self.sort_by do |file|
file.scan(/\d+|[a-z]*/).map { |el| el.to_i }
end
end
end
filenames = [
"foo-1.10.2.ext",
@ivanbrennan
ivanbrennan / tower_of_hanoi.rb
Last active December 24, 2015 10:39
Tower of Hanoi
# tower_of_hanoi.rb
a = [1,2,3,4]
b = []
c = []
# Move the disks from one peg to another following the rules of Hanoi.
#
# number_of_disks - the total number of disks
# from - the starting peg
@ivanbrennan
ivanbrennan / blog_post_scheduler.rb
Created October 2, 2013 01:58
Blog post scheduler
# create a method called create_groups that, given an array of student names,
# a group size, and a number of groups, will return an array of groups of
# of students with no student in adjacent groups
def create_groups(students, group_size, num_of_groups)
out_of_order = students.shuffle
groups = []
index = 0
num_of_groups.times do
group = []
group_size.times do