Created
September 30, 2013 20:25
-
-
Save davidbella/6769700 to your computer and use it in GitHub Desktop.
Ruby: Reverse words and sentences; Hash practice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | |
end | |
end | |
recipes = { | |
:cake => ["eggs", "milk", "sugar"], | |
:steak => ["steak"], | |
:hamburger => ["bun", "ground beef", "ketchup", "another burger"] | |
} | |
recipes.each do |type, food_list| | |
puts "#{type}: #{food_list}" | |
end | |
people = { | |
:greg => { | |
:essays => [ | |
"essay_1", | |
"essay_2", | |
"essay_3" | |
], | |
:favorite_colors => [ | |
"red", | |
"blue" | |
] | |
}, | |
:kyle => { | |
:essays => [ | |
"essay_4", | |
"essay_5", | |
"essay_6" | |
], | |
:favorite_colors => [ | |
"green", | |
"blue" | |
] | |
}, | |
:stephanie => { | |
:essays => [ | |
"essay_7", | |
"essay_8", | |
"essay_9" | |
], | |
:favorite_colors => [ | |
"blue", | |
"orange" | |
] | |
} | |
} | |
puts people |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def reverse_each_word(sentence) | |
reversed = (sentence.split.map do |word| | |
word.reverse | |
end).join(' ') | |
end | |
puts reverse_each_word("This is a sentence to reverse the words of") | |
# Misread the assignment and thought it was this | |
def reverse_word_order(sentence) | |
reversed = [] | |
sentence.split.each do |word| | |
reversed.unshift word | |
end | |
reversed.join(' ') | |
end | |
puts reverse_word_order("This is a sentence to reverse the word order of") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment