Ruby on Rails 5 API

After using Ruby on Rails 5 I do not want to go back, I’ve fell in love. In my opinion this was one of the best updates in software. My congratulations to the core team and all the contributors.

Bcrypt gem is included and it is not necessary the old manual configuration steps which spares a lot of time and makes us focused on other important things.

In some commands the old rake is gone, such as:

rails db:migrate
rails test

To build the API:

rails new tasks --api

Then you add this code to the Gemfile

gem 'active_model_serializers'

After, you create this file and write the code that follows

config/initializers/active_model_serializers.rb

ActiveModel::Serializer.config.adapter = :json_api

Run this command to update the gems

bundle install

Create both Users and Todos using scaffold

rails generate scaffold User email
rails g scaffold Todo title completed:boolean order:integer category:string user:references

The scaffold option makes a lot of things for us but there is more work to do, some configuration, more code and in my case I added authentication.

The code can be checked in my github through the following link: https://github.com/Adnilson/tasks

Solving Algorithms – Largest prime factor

Today I solved this algorithm that is the third problem in Project Euler.
This still need some improvement, because even when the last prime number is found it still counts and takes a really long time when given big numbers.

require 'prime'

def largestPrime(num)
  result = 0
  (2...num).each do |i|
    result = i if (num % i) == 0 && i.prime?
  end
  puts result
end

largestPrime(13195)

I will update this code as soon as I find a better solution but for now it works.

Design Patterns

I’ve got this book last week. It’s the most expensive I’ve ever bought and I do not regret!
I am still in the beginning and learned what are Design Patterns, the catalog of the 23 design patterns in this book and how they solve problems. After, will be shown how to select and use a design pattern, a case study and then we delve into each one of the patterns. The authors explain Object Oriented Design and how objects interact with each other as an introduction.

IMG_20160726_143709727

The good thing about this is that the book has a guide and is suited for rookie readers which is awesome!

Solving Algorithms – Bubble Sort

Why are solving algorithms Adnilson, shouldn’t you work on building applications? Yes, you are right! The thing with algorithms is that it helps you solve problems and are good to make your mind look for solutions. This way you build a blueprint and whenever you get a problem you tackle the problem instead of being tackled by it.

In Ruby things sometimes are too easy and today I will go back to school and review the bubble sort, this algorithm is not the most efficient way of sorting but is a simple and cool exercise.
In Ruby if you want to sort say, an array you can do it by doing array.sort and bam! There it is.
The code:

 

length = array.length - 1
loop do
  swapped = false
  (0...length).each do |i|
    if array[i] > array[i+1]
      array[i], array[i+1] = array[i+1], array[i]
      swapped = true
    end
  end
  break if !swapped
end

puts array

I guess the code speaks for itself. In a lower level language the program would have an extra variable to store the value of one of the array contents in order to swap.
The first time I did this was in Pascal, then Fortran in college.