viernes, 26 de julio de 2013

Ruby learnings

Well in this post I'm going to show some simple examples to start with ruby. These are a really simple examples and this post is oriented to people with any experience in ruby. Also I'll be posting more examples in a while but there will be in git I'll put the link at the end of the post.

So, basically I'm going to show you how to make a class and into it there are going to be some examples in ruby, like factorial function, the first hello, find out if a number is prime and given an array with names print those that start with a determined letter.

Here is the complete code:

_________________________________________________________________________________

class Examples

  def initialize
    @people = ["Carlos","Maria", "Juan", "Sonia", "Sandra" ]

  end

  def call_examples
    hello_ruby
    print "The result of 5! is #{factorial(5)} \n"
    print "Is 7 prime? #{ is_prime_number?(7)} \n"
    print "Is 16 prime? #{ is_prime_number?(16)} \n"
    starts_with?("S")
  end

  def hello_ruby
    print "First hello in ruby!!\n"
  end

  def factorial(number)
    result = 1
    if number > 0
      result = number * factorial(number - 1)
    else
      result = result * 1
    end
    return result
  end

  def is_prime_number?(number)
    divisor = 2
    if number == 2
      return true
    end
    while divisor < number
      if number%divisor == 0 and not number == divisor
        return false
      end
      divisor += 1
    end
    return true
  end

  def starts_with?(letter)
    @people.each {|x| match?(letter, x)}
  end

  def match?(letter,name)
    if name.start_with?(letter)
      print "#{name} starts with #{letter} \n"
    end
  end

end
example = Examples.new
example.call_examples
_________________________________________________________________________________

Let's see each method:


def hello_ruby
    print "First hello in ruby!!\n"
end 

This one is the first example that everybody learn when starting a new language, so it is very simple. The command print just put what you want into the console. You can change the things between "".


 def factorial(number)
    result = 1
    if number > 0
      result = number * factorial(number - 1)
    else
      result = result * 1
    end
    return result
  end

Factorial, factorial function is about multiply the given number by its predecessor until it is 0.
So for example if we have 7! we should do 7*6*5*4*3*2*1*1, the last one is because when the number is zero the result is 1.


def is_prime_number?(number)
    divisor = 2
    if number == 2
      return true
    end
    while divisor < number
      if number%divisor == 0 and not number == divisor 
        return false
      end
      divisor += 1
    end
    return true
  end

This function as its name says it tells us if a number is prime. The important part of this method is the while cycle, where I search for the modulo of the number%divisor because if it is equal to 0 the number is not prime.


def starts_with?(letter)
    @people.each {|x| match?(letter, x)}
  end

  def match?(letter,name)
    if name.start_with?(letter)
      print "#{name} starts with #{letter} \n"
    end
  end

And this last example is about using an array, what I do is to compare each first letter of each element of the array with a given letter if the element starts with the letter it will be printed.

Note: This is the link to github in https://github.com/fernandacoto/Ruby_Examples , in that repository I hope to continue updating examples each time more complex.





No hay comentarios:

Publicar un comentario