First week saas-class

I've been studying hard this weekend to keep on track with Video lecture releases of Saas course. I've also started learning Ruby, and I'm happy for doing it, I'm impressed with the verbosity of the language, if you build good code it's very straight forward to understand. :-)

I finished some parts of the first assignment but some of them need to be refactored to improve conciseness.

First conclussions of the language comparing with Python, in Ruby everything is a method, and in a function the last statement is what you return, you don't need to use the reserved word return. I was surprised because zero and empty arrays are true in conditional statements, but I supposed you get used to that processing in a different way that kind of situations.

You can call a function without parentheses and you can remove more unuseful characters sometimes. Although in Python is different, when you write the name of the function you get a function object, in Ruby you get the result of the execution of that function.

I'll show you the first part of the assignment to understand my points if you haven't study any ruby code.


=begin
    Part1 of the first asignment @javaguirre
=end

def palyndrome?(string)
    string.downcase.gsub(/[^\w+]/, "") == string.downcase.reverse.gsub(/[^\w+]/, "")
end

def count_words(string)
    result = {}
    string.downcase.gsub(/[^\w+]/, " ").split(" ").each do |word|

        if result[word].nil?
            result[word] = 1
        else
            result[word] += 1
        end
    end
    puts "count_words #{string} # => #{result}"
end

puts palyndrome? "A man, a plan, a canal --- Panama"
puts palyndrome? "Madam, I'm Adam!"
puts palyndrome? "Abracadabra"
puts count_words "A man, a plan, a canal --- Panama"

I will try to post a more detailed article about my Ruby learning and Saas class soon!

Javier Aguirre

Read more posts by this author.

comments powered by Disqus