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.


def strings_to_lengths(strings)
  # You write this part. Use each_with_object.

# 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 }

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


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(""))
  speakers_hash =
  doc.xpath("//SPEAKER").map {|speaker| speakers_hash[speaker.text] += speaker.xpath("..//LINE").count} 
  # print values and keys ...

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.