(Repost from Ruby-Forum.com. Sorry, but I'm really etching to continue learning Ruby..)
In w(p)gtr I have found myself facing a code which, for the life of me,
I just could not understand a few thing in! Seeing as this is the
closest board realting to the subject, I shall post my question here -

The code I speak of is -

class String

   # The parts of my daughter's organ
   # instructor's name.
   @@syllables = [
     { 'Paij' => 'Personal',
       'Gonk' => 'Business',
       'Blon' => 'Slave',
       'Stro' => 'Master',
       'Wert' => 'Father',
       'Onnn' => 'Mother' },
     { 'ree'  => 'AM',
       'plo'  => 'PM' }
   ]

   # A method to determine what a
   # certain name of his means.
   def name_significance
     parts = self.split( '-' )
     syllables = @@syllables.dup
     signif = parts.collect do |p|
       syllables.shift[p]
     end
     signif.join( ' ' )
   end

 end

Now, what I could not understand is:

  • Why is @@syllables divided into two hashes? Why is it an array in the
    first place?
  • And secondly, "syllables.shift[p]". Playing around I gathered that the
    .shift[p] method returns the result of hash[p] and extracts the pair
    from the hash. Yet I was told that the [p] part isn't an argument! So I
    can't seem to fully understand this code..

It doesn't seem to work when I join the two hashes into one, and, again,
I can't understand why.

It is probably just a little thing I'm missing, or some-such event. If
anyone could be of assistance, I will be very grateful.

Yours,
-Gill

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.