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

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 – Title Capitalisation

Today I encountered an algorithm to solve and the amazing Ruby syntax made me solve this problem with a succinct code with testing included.
All words in the title should be capitalised except a few ones like “a”, “the”, “to”, “at”, “in”, “with”, “and”, “but” and “or”.

To make this very short the solution is here:

require 'test/unit'

class MyTest < Test::Unit::TestCase
  test "capitalization" do
    assert_equal title_case("i love solving problems and it is fun!"),
                           ("I Love Solving Problems and It Is Fun!")
    assert_equal title_case("wHy DoeS A biRd Fly?"),
                           ("Why Does a Bird Fly?")
  end
end

def title_case(title)
  lower_list = %w(a the to at in with and but or)
  words = title.downcase.split
  words.each do |word|
    word.capitalize! unless lower_list.include?(word)
  end
  title = words.join(' ')
end

Taking more than you can handle

Some time ago a friend gave me a coding challenge which I decided to make it in Ruby on Rails, I hesitated because was expecting that an Enterprise would give me a task to build something in JavaScript/HTML/CSS, so the time passed. Then I applied for a company that is hiring any kind of developer that understands how the Full Stack works and got another coding challenge. This time in Nodejs and Express. Perhaps I do not need to say that I have to learn a whole new technology to get the challenge working and net the job. Besides all of this I enrolled in a course from Berkeley University of California called SaaS (Software as a Service) and the framework they use is… You got it! Ruby on Rails!
How am I going to do all of this? Since I like to finish what I’ve started (which sometimes is not possible), I feel stressed about this situation and wasn’t expecting all that’s happening. I heard somewhere that it is not about the how but the why. Why do I want to be a Developer? Why do I want to learn? Why am I pushing through all the resistance? Why do I care about Self-Development and read/listen to a lot of books? The answer is simple, I want to become the best, learn with the best, be on the top 3%. Is it simple or easy? I don’t think so. Do I think it is possible? Absolutely.

var and let the difference in JavaScript

Javascrit has something called Hoisting. This is when all variables and functions are loaded before the program runs.

function loadProfiles(userNames){
// some code...

    var loadingMessage = "Hold a sec... Profiles loading!";
// some code...
}

var loadingMessage = "Loading your profile!";

What will happen here is that the first declaration of loadingMessage will be loaded first,then the second declaration will be loaded and overwrite the first. Which will not give us what we want.
To solve this issue ES6 or ES2015 came with a solution: let !

function loadProfiles(userNames){
// some code...

    let loadingMessage = "Hold a sec... Profiles loading!";
//some code...

}

let loadingMessage = "Loading your profile!";

These variables will load locally, saving memory without overwriting each other. If you console.log the loadingMessage variable after the loadProfiles method, the result will be different from if you ran inside the loadProfiles scope.
What is a scope? I’ll tell you later.

Hexadecimal and Colors

This week will be more related to the Front End side of web development. So I thought about colors which are used everywhere. The web is one of the technologies that uses all the human senses, it evolved to become an everyday tool and entertainment.
Today I will teach you how to calculate hexadecimal numbers and how they can be related to numbers.

Hexa-decimal: Hexa comes from the Greek word héx which means six. The decimal I’m sure that you already now.
Decimal numbers run from 0-9.

81 (decimal):
multiply the left by 10, (8x10)
multiply the right by 1, (1x1)
We get 81 decimal.
81 (hexadecimal):
multiply the left by 16, (8x16)
multiply the right by 16, (1x1)
By adding them we get 129 hexadecimal!

When we work with colors particularly RGB, the minimum value is 0 and maximum is 255.
With hexadecimal numbers we work with 3 pairs of Red, Green and Blue, hence RGB.
_ _  _ _  _ _
R     G     B

The minimum pair is 00 and maximum FF. Oh! Letters? Yes, I will show you in a bit how this works. As I said above the decimal numbers are limited to 0-9, so a few engineers had to figure out a way to solve the problem. To get things working they assigned the following values:

A = 10, B = 11, C = 12, D = 13, E = 14, F = 15

Few examples:

#FF0000
Red: (15x16) + (15x1) = 255
Green: (0x16) + (0x1) = 0
Blue: (0x16) + (0x1) = 0
Yes, you got it right, the color is full red! My favorite color.

#83d900
Red: (8x16) + (3x1) = 131
Green: (13x16) + (9x1) = 217
Blue: (0x16) + (0x1) = 0

#efcc3a
Red: (14x16) + (15x1) = 239
Green: (11x16) + (11x1) = 187
Blue: (3x16) + (10x1) = 58

And that’s it! You know how to calculate hexadecimal number and how they are related to numbers.

What are Programming Languages ?

2 weeks ago I went to an Equal Experts Conference and after while mingling, in a conversation with one of the organizers, some were saying that language X was better than language B, and his response was: “Languages are just tools to build applications to serve the world”. That just killed.

And this is the message about this post, Programming Languages are just tools, you use them and not get used by them.

Have a nice day.