Ruby Challenges - Analyzing Shakespeare

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 :smile:.

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.

4 Likes

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.