Ruby Monk Notes

Now I’m just keeping notes of my learning process while moving through RubyMonk.

Thus far, RubyMonk has proven to be a slightly more challenging learning experience as the system demands more of your intuition. They’ve done an excellent job at allowing the learner to get unstuck with a hint. It’s good that they do that, but they also make it clear that using the hints too often will cause you to not learn the material well enough.

I thought it was fairly funny after I answered a question that counts the length of strings in an array. I wrote code that works, it’s commented below, but the answer was much shorter. Check this out, it’s embarrassing for me:

# My code:
# def length_finder(input_array)
#   length_array = []
#   idx = 0
#   while idx < input_array.length
#     length_integer = input_array[idx].length
#     length_array = length_array.push(length_integer.to_i)
#     idx += 1
#   end
#   return length_array
# end
# puts length_finder(['Ruby','Rails','C42']).to_s
# Their code: 

def length_finder(input_array) {|element| element.length}

This is actually one of my largest hangups is this style of { |x| x do y} code. I don’t quite understand it so I’m grateful that Ruby Monk is making it a priority now.

Here’s how to use that syntax to delete the even numbers:

[1,2,3,4,5,6,7,8,9].delete_if{ |i| i % 2 == 0 }

Simple. Clean. I like it.

Interesting. From all my studying earlier, we never used >> to push elements to arrays. RubyMonk says it’s the most common way, but I’ve always used .push(“string example”) to push elements to arrays. Good to know.

Leave a Reply

Your email address will not be published. Required fields are marked *