Skip to content

Instantly share code, notes, and snippets.

View mecampbellsoup's full-sized avatar

Matt Campbell mecampbellsoup

View GitHub Profile
@mecampbellsoup
mecampbellsoup / my_each.rb
Created September 27, 2013 05:00
Replica of Ruby's Array#each method. Appropriately dubbed "my_each"... :) Throwing in my_collect for shits and giggles.
class Array
def my_each
i = 0
while i < self.length
yield(self[i])
i += 1
end
self
end
@mecampbellsoup
mecampbellsoup / conf_greeting.rb
Created September 27, 2013 12:50
Speaker greeting method
names = %w(Edsger Ada Charles Alan Grace Linus Matz)
rooms = %w(1 2 3 4 5 6 7)
def say_speaker_greeting(name, name_list, room_list)
i = name_list.index(name)
#"Hello, (name)! You'll be assigned to room (room)!"
# set room assignments... could do it randomly using sample
puts "Hello, #{name}! You'll be assigned to room #{room_list[i]}!"
end
@mecampbellsoup
mecampbellsoup / tweet_shortener.rb
Created September 27, 2013 13:51
Tweet shortener!
tweets = ["test", "hey guys, can anyone teach me how to be cool? I really want to be the best at everything, you know what I mean? Tweeting is super fun you guys!!!!", "OMG you guys, you won't believe how sweet my kitten is. My kitten is like super cuddly and too cute to be believed right?", "I'm running out of example tweets for you guys, which is weird, because I'm a writer and this is just writing and I tweet all day. For real, you guys. For real.", "GUISEEEEE this is so fun! I'm tweeting for you guys and this tweet is SOOOO long it's gonna be way more than you would think twitter can handle, so shorten it up you know what I mean? I just can never tell how long to keep typing!", "this tweet shouldn't be shortened to too two"]
substitutes = { 2 => ["to", "two", "too"], 4 => ["for", "four"], "be" => ["b"], "you" => ["u"], "at" => ["@"], "and" => ["&"] }
def check_each_word_against_substitutes(tweets, substitutes)
tweets.each do |tweet|
word_count = 0
if tweet.length > 140
amended_tweet = tw
@mecampbellsoup
mecampbellsoup / normalize_phone_number.rb
Created September 27, 2013 15:16
Phone # formatter!
require 'awesome_print'
require 'pry'
def normalize_phone_number number_string
numbers_only = []
if number_string.size >= 10
number_string.gsub!(/\D/, '')
number_string.each_char do |char|
if char.to_i.is_a?(Numeric)
numbers_only << char
@mecampbellsoup
mecampbellsoup / jukebox.rb
Last active December 24, 2015 02:49
Jukebox game machine thingy!
require 'pry'
# Download Gist: https://gist.github.com/scottcreynolds/e6534b284373efe0ba6e/download
# Build a Jukebox
# create a file jukebox.rb
# When that program is run, it should introduce itself
# to the user and accept input from the user using the gets command.
@mecampbellsoup
mecampbellsoup / game_of_life.rb
Last active December 24, 2015 04:29
My rendition of Conway's Game of Life...
require 'awesome_print'
require 'PP'
# If cell is alive then it continues to stay alive if it has
# 2 or 3 alive neighbors. If the cell is dead then it comes
# back to life only if it has exactly 3 alive neighbors.
# @alive is a Boolean value in each cell that changes through time
# @alive has neighbors, eight to be exact
# if @alive = true, then it becomes false only if the # of neighbors > 3 or < 2 (it remains true if neighbors == 2 || neighbors == 3)
# if @alive = false, then it becomes true only if the # of neighbors == 3
# Conway's Game of Life rules:
## If cell is alive then it continues to stay alive if it has 2 or 3 alive neighbors
## If cell is alive then it dies if it has <2 or >3 neighbors
## If cell is dead then it comes back to life only if it has exactly 3 alive neighbors.
def generate(world_size)
world = []
world_size.times do
row = []
world_size.times do
require 'awesome_print'
require 'pry'
# Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
# 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
# By considering the terms in the Fibonacci sequence whose terminal value does not exceed four million, find the sum of the even-valued terms.
def fibonacci(terminal_value_not_to_be_exceeded)
require 'awesome_print'
def reverse_each_word(sentence)
reversed_array = sentence.split(" ").collect do |word|
word.reverse
end
reversed_array.join(" ")
end
# reverse_each_word("Hello there, and how are you?")
def apple_picker(array_of_fruits)
#array_of_fruits.select { |fruit| fruit if fruit == "apple" }
array_of_fruits.collect do |fruit|
fruit if fruit == "apple"
end.compact
end
p apple_picker(["apple", "orange", "apple"]) #=> ["apple", "apple"]
# select vs. collect