← Back to Upcase

Ruby Challenges - Analyzing Shakespeare


(Upcase ) #1

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.


(Ben Orenstein) #2

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.


(Aaron Mc Adam) #3

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


(Ben Orenstein) #4

Good work! That’s the approach I had in mind.


(thedanotto) #5

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.


(DiSalvio) #6

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?


(thiagoa) #7

I guess yes @DISalvio, the characters should be listed exactly as they appear in the SPEAKER tag.


(Nicholai de Guzman) #8

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.