08 fevereiro 2015

Changing git remote repos with Capistrano - Repository not found

On changing where our remote git repos were stored we got the error Repository not found When trying to run cap deploy with capistrano. We had changed the remote origin to the new repos locally with git remote rm origin git remote add origin It turned out the you need to change the remote on the server you are deploying to in the folder rails_app/shared/cached-copy as it still references the old remote. So the fix on the production server was. cd rails_app/shared/cached-copy git remote rm origin git remote add origin

02 fevereiro 2015

Ruby: Singleton Class

Every object in ruby is associated with two classes: The class which instantiated it and an anonymous class. The anonymous class is called "singleton class".
You're probably familiar with this kind of code:
class Person
  class << self
    def count
      @count ||= 0
    end
  end
end

which is the same as:

class Person
  def self.count
    @count ||= 0
  end
end

Everything time we inject a method into an object, they are added as singleton methods. But you should know that those methods belong only to the object in which they were defined.

string = 'bernardofire'
another_string = 'foobar'

def string.first_letter
  self[0]
end

string.first_letter #=> "b"
another_string.respond_to?(:first_letter) #=> false

We can also use Object#singleton_methods to prove that #first_letter is a singleton method:
string.singleton_methods
#=> ["first_letter"]

another_string.singleton_methods
#=> []

11 dezembro 2014

Big O Notation

Big O notation describes the worst case scenario for an algorithm.

O(1):
Doesn't matter the size of the dataset, it will always execute in the same time.

def change_first_element(array, element)
 array[0] = element
end

O(N):
It takes a linear time range.

def looper(array)
  array.each {|e| puts e }
end
O(N2): It means that the function will perform proportionally to the square of the input data size. Bubblesort is an example of O(N2) algorithm, it uses two chained loops that irate over the data.
class Array
  def bubblesort!
    length.times do |j| # iterates over the dataset
      for i in 1...(length - j) # nested iteration
        if self[i] < self[i - 1]
          self[i], self[i - 1] = self[i - 1], self[i]
        end
      end
    end
    self
  end
end 

Sorting Algorithms

I'm current reading 'Essential Algorithms' and I've just finished chapter 6, which talks about sorting algorithms. Below is my summary of this chapter. Please, correct me if I misunderstood something.

O(N2):
  - Insertionsort
    For each element it searches the whole array before the element's index
    and puts it in the right place and move all the next elements to the right.
  - Selectionsort
    Searches for the smallest element and put it in the right place and then
    move all the others to right.
  - Bubllesort
    Goes through the array, if the current element is bigger than the next one,
    swaps it. It does that until there's no more elements to swap.
O(N Log N):
  - Heapsort
    It uses a data structure called a heap (the parent must be bigger than the
    children) and it is useful for sorting a binary tree in an array. It is not parallelizable.
  - Quicksort
    It takes a dividing item and separate the items between before and after
    the dividing item. It is parallelizable. Worst case is O(N2). Uses recursion.
  - Mergesort
    It separates the array always in half. It is parallelizable. Uses recursion.
  * Stable Sorting
Sub O(N Log N):
  - Countingsort
    Only works if the items are integers in a relatively small range. The ideia is
    to count the number of items in the array that have each value. O(N + M).
  - Bucketsort
    It devides the items in buckets and call it recursively or uses another algorithm
    to sort each bucket and then concatenates the bucket's contents back into the
    original array. O(N + M).

18 agosto 2014

Speed up Rails, Speed up Your Code

I had just finished watching Tenderlove's (Aaron Patterson) talk called "Speed up Rails, Speed up Your Code". He talks about caching approach, ActiveRecord and the improvements "they" made to speed up rails over time. I really recommend people to watch it. Here are some tips/thoughts he gives in the end of the talk:

- Cache Invariants 
    "Any time you find a calculation you're doing over and over again, and it always ends up being the same, you should cache it"
- Eliminate Objects 
    "No code is faster than no code"
- Limit Types 
    "Limit the types of objects that your function handles"
- Rails 4.2 will be the fastest ever! o/ 

 Anyways, check his talk at Confreaks.

29 janeiro 2014

Shebang: How the System Knows a File Is a Ruby Script

Compiled programs have the information about how the operating system should start the program in the executable file. In scripting languages like ruby, python.. the operating system needs another way to know how to start the script. On Unix systems, this is done in the first line of code, known as Shebang.

The shebang begins with #, followed by ! and the absolute path of the interpreter which will execute the script:
#!/usr/bin/ruby
puts "Hello World!"

17 janeiro 2014

SQL: Learning it the hard way

I'm current reading Learn SQL The Hard Way, It's the first book from "learncodethehardway" that I read. I really liked their teaching approach, they don't waste time, the entire book is really practical. If you want to learn some SQL, I suggest you to read THIS book.
Here is some basic SQL operations you'll learn in the beginning of the book:

Creating basic tables:
CREATE TABLE person (
  id INTEGER PRIMARY KEY,
  first_name TEXT,
  last_name TEXT,
  age INTEGER
);

CREATE TABLE pet (
  id INTEGER PRIMARY KEY,
  name TEXT,
  breed TEXT,
  age INTEGER,
  dead INTEGER
);
Creating a relation table between person and pet:
CREATE TABLE person_pet (
  person_id INTEGER,
  pet_id INTEGER
);
Inserting data:
INSERT INTO person (id, first_name, last_name, age) VALUES (0, 'bernardo', 'fire', 17);
INSERT INTO pet (id, name, breed, age, dead) VALUES (0, 'zeus', 'dog', 6, 0);
Inserting referential data:
INSERT INTO person_pet (person_id, pet_id) VALUES (0, 0);
Selecting Data:
SELECT * FROM person;
SELECT name, breed FROM pet;
SELECT name FROM pet WHERE breed = "unicorn";
SELECT * FROM pet WHERE dead = 0;
Select Across Many Table:
SELECT pet.id, pet.name, pet.age, pet.dead
    FROM pet, person_pet, person
    WHERE
    pet.id = person_pet.pet_id AND
    person_pet.person_id = person.id AND
    person.first_name = "bernardo";
Deleting Data:
DELETE FROM pet WHERE first_name = 'bernardo';
Droping table:
DROP TABLE person;
Deleting using other tables:
DELETE FROM pet WHERE id IN (
    SELECT pet.id
    FROM pet, person_pet, person
    WHERE
    pet.id = person_pet.pet_id AND
    person_pet.person_id = person.id AND
    person.first_name = "bernardo"
);
Removing the relation table since the pet doesn't longer exist.
DELETE FROM person_pet WHERE pet.id NOT IN (
    SELECT id FROM pet
);