Today, I skyped with my prework coordinator Nick to go over my answers to the ruby assessment I took on Sunday. There were some useful methods that I completely forgot about but are very useful. I'm writing down the parts I got wrong for the future.
#1. Iterate through 'names' and return a new array with each name capitalized.
names = ["nick", "mike", "shehzan", "gilbert"]
my answer was to make a new empty array and use the .each method to iterate through each item in the array and push the capitalized version into the empty one but I could use a more simple approach by utilizing the .map method.
names.map{|x| x.capitalize}
^ which will return the new array we want
#2. Return the cast members of The Sopranos as a single string separated by commas.
tv_shows = {
"Mad Men" => {:network => "AMC", :cast => ["John Hamm", "John Slattery", "Elizabeth Olson"]},
"Dexter" => {:network => "Showtime", :cast => ["Michael C. Hall", "Jennifer Carpenter", "CS Lee"]},
"The Sopranos" => {:network => "HBO", :cast => ["James Gandolfini", "Michael Imperioli", "Edie Falco"]}
}
my answer was to first get inside the first hash and access the third key value pair and then access the value of the key :cast, then create an empty string and use the .each method again to go through each and recursively concatenate to the string but a simpler way is to use the .join method.
tv_shows["The Sopranos"][:cast].join(",")
^it's basically the opposite of the .split method
And review some object oriented programing stuff
#1. Iterate through 'names' and return a new array with each name capitalized.
names = ["nick", "mike", "shehzan", "gilbert"]
my answer was to make a new empty array and use the .each method to iterate through each item in the array and push the capitalized version into the empty one but I could use a more simple approach by utilizing the .map method.
names.map{|x| x.capitalize}
^ which will return the new array we want
#2. Return the cast members of The Sopranos as a single string separated by commas.
tv_shows = {
"Mad Men" => {:network => "AMC", :cast => ["John Hamm", "John Slattery", "Elizabeth Olson"]},
"Dexter" => {:network => "Showtime", :cast => ["Michael C. Hall", "Jennifer Carpenter", "CS Lee"]},
"The Sopranos" => {:network => "HBO", :cast => ["James Gandolfini", "Michael Imperioli", "Edie Falco"]}
}
my answer was to first get inside the first hash and access the third key value pair and then access the value of the key :cast, then create an empty string and use the .each method again to go through each and recursively concatenate to the string but a simpler way is to use the .join method.
tv_shows["The Sopranos"][:cast].join(",")
^it's basically the opposite of the .split method
And review some object oriented programing stuff