Prefer simple methods

screenshot

Long methods vs simple ones. What’s the problem?

Long methods’ issues:

  • usually does too much and “knows” too much
  • as a consequence, it breaks the Single Responsibility Principle
  • it is harder to grasp what it does when you read it
  • more changes needed to add or amend functionality
  • you have to write pretty complex tests
  • and you need a lot of them to cover all of the possible permutations

Exception vs StandardError

# RuntimeError < StandardError < Exception < Object

begin
  require 'does/not/exist'
rescue Exception => e
  "it is an Exception"
end
# => "it is an Exception"

begin
  require 'does/not/exist'
rescue StandardError => e
  "it is an Exception"
end
# => LoadError: cannot load such file -- does/not/exist

What is the difference in results between this

raise "Oups" rescue "Hi"

and this?

require 'does/not/exist' rescue "Hi"

And why the results are different. Let’s figure it out step by step.

Let’s implement ‘Stack’ in Python

a = Stack()     # []
a.push('one')   # ['one']
a.push('two')   # ['one', 'two']
a.top()         # 'two'
a.push('three') # ['one', 'two', 'three']
a.top()         # 'three'
a.size()        # 3
a.pop()         # 'three'  ['one', 'two']
a.pop()         # 'two'    ['one']

This is the API I want to have for the ‘Stack’ data structure.

Let’s implement it in Python step by step.

JavaScript like object in Ruby on Rails

screenshot

One day, when I was explaining how the routing > params > controllers > views work, I wanted to keep things as simple as possible, so I needed a way to flexibly “mock” AR objects.

And the JavaScript-like objects would be a good-enough solution for it.

let obj = {}
obj.some_field
// undefined - N.B. no error raised
obj.some_field = "some value"
obj.some_field
// "some value" - JS "magic"

In contrast, the Ruby’s Object does not allow it.

Unleash your AI to rule the StarCraft2 (on macOS)

screenshot

This a bit of pretentious title is about the setup of the great PySC2 library which makes it possible to “rule” the StarCraft II with your own AI.

“Scientifically” speaking, the PySC2 allows for Reinforcement Learning agents to interact with the StarCraft II game. The library does it by exposing the StarCraft II Machine Learning API as a Python RL Environment.

Dissecting of the ‘rails new’ command

screenshot

If we run rails new -h we see that we can disable generating of some parts of a new rails project.

In this article I am going to dissect these particular options:

-S, [--skip-sprockets]     # Skip Sprockets files
-J, [--skip-javascript]    # Skip JavaScript files
    [--skip-turbolinks]    # Skip turbolinks gem
-O, [--skip-active-record] # Skip Active Record files