The structure of the Tealeaf bootcamp course I’m taking is split into 3 modules each I’m told is progressively harder than the previous. I am on the first module ‘Introduction to Ruby and Web Development’ which is split into a pre course followed by 4 lessons.

I had a bit of a moment today which made me think that although I’m not in the classroom environment a self paced online course may actually be a better option anyway. I ran into a problem where I was just struggling to understand a peice of code I wrote:

 1 words = ["demo", "none", "tied", "evil", "dome",
 2          "mode", "live", "fowl", "veil", "wolf", 
 3          "diet", "vile", "edit", "tide", "flow", "neon"]
 4 
 5 result = {}
 6 
 7 words.each do |word|
 8   key = word.split(//).sort.join
 9   if result.has_key?(key)
10     result[key] << word
11   else
12     result[key] = [word]
13   end
14 end
15 
16 result.each do |k,v|
17   puts "--------"
18   puts "#{k}: #{v}"
19 end

Basically the above code is taking the letters from each string in the ‘words’ array, sorting them in alphabtical order as an anagram and then adding them to a hash. For example the string ‘none’ is first split as ‘enno’ and then the program checks to see if the key ‘enno’ already exists and if it does adds the word ‘none’ to that key. If the key doesn’t exist it will create a new key using the key and then asign ‘none’ to the newly created key. phew!

The output looks something like this:

——–
demo: [“demo”, “dome”, “mode”]
——–
enno: [“none”, “neon”]
——–
deit: [“tied”, “diet”, “edit”, “tide”]
——–
eilv: [“evil”, “live”, “veil”, “vile”]
——–
flow: [“fowl”, “wolf”, “flow”]

I understood everything except the else conditional starting on line 25. Thanks to the friendly folk in the #ruby irc channel it was explained to me that:

result[key] = [word]

looks to see if key already exists and if it doesn’t it will create it and then asign word to it.

Taking time to understand before moving on

Even though I started the day planning on getting stuck into lesson 1 I spent most of my day going over the pre-course stuff a second time to make sure I didn’t miss anything the first time. I also spent a good hour or two taking the time to understand that specific peice of code.

Maybe if I was in the classroom environment there would have been a pressure to move on to keep pace with the course and I might have felt that I was moving on before I was ready. On the other hand maybe one of the tutors would have sat with me until I understood fully the code and it would only have held me up for 20 minutes.

Thanks to:

Each time I post I’ll aim to thank anyone that has helped me along the way. Today workmad3 DefV & apeiros helped me in the ruby channel on irc. I was struggling to understand the syntax for adding a key and a value based on a conditional if else statement.