Skip to content

Instantly share code, notes, and snippets.

View JoshuaFields's full-sized avatar

Josh Fields JoshuaFields

  • Bindable
View GitHub Profile
@JoshuaFields
JoshuaFields / deaf_grandma
Created December 15, 2014 17:19
Deaf Grandma
puts "Hi! Say something to Grandma below."
text = nil
while true
text = gets.chomp
if text == "BYE"
puts "Grandma didn\'t hear you. Say \"BYE\" again."
elsif text == text.upcase
@JoshuaFields
JoshuaFields / full_name_greeting.rb
Created December 26, 2014 21:39
Part of exercise 5.6 in Chris Pine's book
puts "What is your first name?"
first=gets.chomp
puts "What is your middle name?"
middle=gets.chomp
puts "What is your last name?"
last=gets.chomp
puts "Hello " + first + " " + middle + " " + last + ". You have an INCREDIBLE name!"
@JoshuaFields
JoshuaFields / better_number.rb
Created December 26, 2014 23:24
Part of exercise 5.6 in Chris Pine's book
puts "What is your most favoritest number in the world?"
fav_number = gets.chomp
puts "Your favorite number is " + fav_number + "? Really?"
new_fav = fav_number.to_i + 1
puts "I think " + new_fav.to_s + " is a much better number."
@JoshuaFields
JoshuaFields / name_length.rb
Created December 26, 2014 23:43
An exercise in chapter 6 of Chris Pine's book
puts "What is your first name?"
first=gets.chomp
puts "What is your middle name?"
middle=gets.chomp
puts "What is your last name?"
last=gets.chomp
puts "Hello " + first + " " + middle + " " + last + "."
name_length = first.length + middle.length + last.length
puts "You have " + name_length.to_s + " letters in your name."
@JoshuaFields
JoshuaFields / angry_boss.rb
Created December 27, 2014 03:21
An exercise in chapter 6 of Chris Pine's book meant to demonstrate the upcase method.
puts "This is your boss. What did you want to meet about?"
response = gets.chomp
puts "WHADDAYA MEAN \"" + response.upcase + "\"" + "?!? YOU\'RE FIRED!!"
@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)
@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 / 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 / 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 / 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)