This topic is for the Analyzing Shakespeare exercise in the Ruby Challenges trail. Post any questions, corrections, or pointers you have to share with other Upcase subscribers.
Continuing the discussion from New trail: Ruby Challenges:
I think the root issue is that youāre not sure how each_with_object
works. Thatās okay, by the way .
Letās simplify the example a little bit. Iām going to give you a simpler challenge that I think will help you understand things.
Write a method that takes an array of strings and returns a hash where the keys are the strings, and the values are their lengths.
Example:
def strings_to_lengths(strings)
# You write this part. Use each_with_object.
end
# Expected results:
strings = ["ben", "eric", "nathan"]
strings_to_lengths(strings) # Returns: { "ben" => 3, "eric" => 4, "nathan" => 6 }
I think when you solve this, youāll be well on your way to understanding how your more-complicated example works.
Hope itās ok to try this:
def strings_to_lengths(strings)
strings.each_with_object({}) { |string, output| output[string] = string.size }
end
I wonder if thereās a nicer way to do this
Good work! Thatās the approach I had in mind.
Hey @benorenstein and @christoomey
Iām personally finding these challenges really difficult. Which isnāt a complaint as Iām sure thatās part of the goal (Challenges is in the title, haha). But one of the reasons Iām finding them difficult is due to how rusty I am with Ruby. Writing Ruby from within a Rails framework has seemingly weakened my pure ruby skills.
Iād love to see a Ruby Refresher as an introductory lesson to this particular trail. I know it would have helped me a great deal with the expectations of the upcoming challenges and getting my mind back into Ruby. To me, easy
means writing a function or two, writing a test and being complete. But I looked at some of the featured answers, and thereās a lot of code, classes, tests and functionality going into analyzing this book. And understanding how all those things fit together in the context of Ruby, seems far from easy. While the underlying code that actually analyzes the book may be easy, getting to that point, seems challengingā¦because Iām rusty.
Overall, Iām loving the challenges, especially since theyāve identified such a weak point in my skills. Iād love to see moreā¦ even easier ones.
I have a question: There is one line that has MacBeth & Lennox listed as speakers. My output thus has a line that reads
ā¦
1 MACBETHLENNOX
ā¦
Is this acceptable? Or should I count the line for both Macbeth & lennox?
I guess yes @DISalvio, the characters should be listed exactly as they appear in the SPEAKER tag.
My solution uses the Nokogiri gem, and the āmeatā of it is only a few lines:
def run
doc = Nokogiri::XML(open("http://www.ibiblio.org/xml/examples/shakespeare/macbeth.xml"))
speakers_hash = Hash.new(0)
doc.xpath("//SPEAKER").map {|speaker| speakers_hash[speaker.text] += speaker.xpath("..//LINE").count}
# print values and keys ...
end
This is my first version, which I committed so that I can look at what others have done.
I see that the featured solution does not use Nokogiri, and seems to be more general, meaning that there are methods and helper methods.
May I get more guidance on how I should approach the challenge?
Thank you.