Skip to content

Instantly share code, notes, and snippets.

@davidbella
davidbella / quiz.rb
Created September 26, 2013 03:29
Ruby: Exercise in converting different units of time
# Write a program that tells you the following:
#
# Hours in a year. How many hours are in a year?
# Minutes in a decade. How many minutes are in a decade?
# Your age in seconds. How many seconds old are you?
#
# Define at least the following methods to accomplish these tasks:
#
# seconds_in_minutes(1)
# minutes_in_hours(1)
@davidbella
davidbella / part11.second_program.txt
Created September 26, 2013 22:12
Ruby: Splitting strings and using each
# Given this List of Songs, Construct Arrays by Artist and Album
# Hint: Make use of the split() String Method
# http://www.ruby-doc.org/core-1.9.3/String.html#method-i-split
# Simple Example of Data Parsing
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 - Holland 1945",
"The Magnetic Fields - Get Lost - You, Me, and the Moon",
@davidbella
davidbella / vowels
Created September 27, 2013 03:19
Ruby: All about finding vowels
def is_vowel_elsif(letter)
if letter == "a"
true
elsif letter == "e"
true
elsif letter == "i"
true
elsif letter == "o"
true
elsif letter == "u"
@davidbella
davidbella / conference
Created September 27, 2013 03:21
Ruby: Conference listing and printing greetings
def print_names(names)
messages = []
names.each do |name|
messages << "Hello, my name is #{name}"
end
messages
end
def assign_room_to_speakers(names)
assignments = []
@davidbella
davidbella / tweet_shortener
Created September 27, 2013 03:48
Ruby: Tweet Shortener
replacements = {
"to" => '2',
"two" => '2',
"too" => '2',
"for" => '4',
"four" => '4',
'be' => 'b',
'you' => 'u',
"at" => "@",
"and" => "&"
@davidbella
davidbella / my_each
Created September 27, 2013 03:52
Ruby: Some examples of yielding to create a custom each method
def my_each (array)
i = 0
while i < array.length
yield array[i]
i += 1
end
end
my_each [1, 2, 3, 4, 5] { |i| puts i**2 }
@davidbella
davidbella / part11.second_program
Created September 27, 2013 03:53
Ruby: Examples of splitting
# Given this List of Songs, Construct Arrays by Artist and Album
# Hint: Make use of the split() String Method
# http://www.ruby-doc.org/core-1.9.3/String.html#method-i-split
# Simple Example of Data Parsing
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 - Holland 1945",
"The Magnetic Fields - Get Lost - You, Me, and the Moon",
@davidbella
davidbella / ruby_phone_format
Created September 27, 2013 13:40
Ruby: Formatting phone numbers (naive)
# 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
# ======================================
@davidbella
davidbella / hashes.rb
Created September 30, 2013 20:25
Ruby: Reverse words and sentences; Hash practice
movies = {
:comedy => ["Dumb and Dumber", "Office Space"],
:action => ["Terminator", "The Matrix"],
:suspense => ["Man on Fire"],
:kids => ["Wall-E", "James and the Giant Peach"]
}
movies.each do |genre, movie_list|
movie_list.each do |movie|
puts "#{genre}: #{movie}"
@davidbella
davidbella / apple_picker.rb
Created September 30, 2013 20:53
Ruby: Holiday Supplies and Apple Picker - working with hashes and select/collect
# Using collect, we have to explicitly set the return value
# And then to clean up - use compact to eliminate nils
def apple_picker(foods)
apples = (foods.collect do |food|
if food == "apple"
food
end
end).compact
end