Last active
August 29, 2015 13:55
-
-
Save oneawayman/8784517 to your computer and use it in GitHub Desktop.
Bloc Web Development Loops Checkpoint
This file contains 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
### The Each Method ### | |
# Add a method named sum_numbers to the Array class. | |
# The method should sum all of the numbers in the Array that it's called on. | |
class Array | |
def sum_numbers | |
start=0 | |
self.each do |sum| | |
start += sum | |
end | |
start | |
end | |
end | |
# Add a method named camel_case to the String class. | |
# The method should return a String with proper camel case conventions. | |
class String | |
def camel_case | |
camelCase = self.split | |
camelCase.each do |word| | |
word.capitalize! | |
end | |
camelCase.first.downcase! | |
camelCase.join | |
end | |
end | |
# Add a method named add_index to the Array class. | |
# The method should take the index value of an element's position and add it to the String in the same position. | |
class Array | |
def add_index | |
addIndex = [] | |
self.each_with_index do |item, index| | |
addIndex << "#{index} is #{item}" | |
end | |
addIndex | |
end | |
end | |
### Title Case ### | |
#Create method to capitalize words except articles | |
class String | |
def title_case | |
articles = ["the", "of", "a", "and"] | |
capital = [] | |
self.split.each_with_index do |word, index| | |
if (articles.include?(word.downcase) && index != 0) | |
capital << word.downcase | |
else | |
capital << word.capitalize | |
end | |
end | |
capital.first.capitalize! | |
capital.join(" ") | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
notes: the difference between ! (bang methods) to the same method without a bang - is that the bang methods change the instance they are running on while the non-bang methods just return a value