Skip to content

Instantly share code, notes, and snippets.

@drewblumberg
Created May 2, 2016 19:38
Show Gist options
  • Save drewblumberg/6dba0fa2b031a18393969f120182a331 to your computer and use it in GitHub Desktop.
Save drewblumberg/6dba0fa2b031a18393969f120182a331 to your computer and use it in GitHub Desktop.
# This function will break up words for us.
def break_words(stuff)
words = stuff.split(' ')
end
# Sorts the words.
def sort_words(words)
words.sort
end
# Prints the first word after popping it off.
def puts_first_word(words)
word = words.shift
puts word
end
# Prints the last word after popping it off.
def puts_last_word(words)
word = words.pop.gsub('.', '')
puts word
end
# Takes in a full sentence and returns the sorted words.
def sort_sentence(sentence)
sort_words(break_words(sentence))
end
# Puts the first and last words of the sentence.
def puts_first_and_last(sentence)
words = break_words(sentence)
puts_first_word(words)
puts_last_word(words)
end
# Sorts the words then prints the first and last one.
def puts_first_and_last_sorted(sentence)
words = sort_words(break_words(sentence))
puts_first_word(words)
puts_last_word(words)
end
puts "Let's practice everything."
puts "You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs."
poem = <<POEM
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explantion
\n\t\twhere there is none.
POEM
puts "--------------"
puts poem
puts "--------------"
five = 10 - 2 + 3 - 6
puts "This should be five: %d" % five
def secret_formula(start_point)
beans = start_point * 500
jars = beans / 1000
crates = jars / 100
return beans, jars, crates
end
start_point = 10000
beans, jars, crates = secret_formula(start_point)
puts "With a starting point of: #{start_point}"
puts "We'd have %d beans, %d jars, and %d crates." % [beans, jars, crates]
# OR
# puts "We'd have #{beans} beans, #{jars} jars, and #{crates} crates."
start_point = start_point / 10
puts "We can also do that this way:"
puts "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point)
sentence = "All god\tthings come to those who weight."
words = break_words(sentence)
sorted_words = sort_words(words)
puts_first_word(words)
puts_last_word(words)
puts_first_word(sorted_words)
puts_last_word(sorted_words)
print sort_sentence(sentence).join(" ").gsub('.', '') << ".\n"
puts_first_and_last(sentence)
puts_first_and_last_sorted(sentence)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment