Ruby

Notes

# with a !, it will be always be created
let!(:country_fr) { WorldCountry.by_name('France') }
# create only in this `describe` only if it's used in the test, like a lazy load
let(:place_of_birth_attributes) { PatientBases::PatientCascade::PLACE_OF_BIRTH_ATTRIBUTES }
  • create method in the test is from engines/online_identity/test/factories/master_patient.rb, must come from a Gem factory_bot
    • The fields are created using test/factories/appointment.rb
  • pundit_user: access control using pundit gem
    • some tests checks all controllers must have pundit_user, that’s why we set an empty object
  • Dry::Validation is a gem that validates the inputs
  • include AnotherClass keyword to get all the methods from AnotherClass
    • extend AnotherClass keyword will import all the static methods from AnotherClass
  • def self.method == static VS def method specific to an instance
  • pry instruction to automatically put a break point then open a rails console (REPL)
  • variable&.method: the &. is a “Safe Navigation Operator”, it lets you call methods on objects without worrying that the object may be nil(Avoiding an undefined method for nil:NilClass error).
  • method(&:symbol) is like static method call in java, .e.g Optional::get
  • ::Namespace → tell ruby to go to the root folder, otherwise, it will think it’s in the same folder
  • ArgumentError is the generic error to use instead of using a custom InvalidArgumentError exception
  • include Validated is used more on active records “world”
  • rails initializer
  • use stdout flush to print directly

Resources

Tutorials

Convention

Ruby Style Guide

Tools

Gems

Most of the libraries are distributed as a gem — a packaged library or application that can be installed with a tool called RubyGems.

  • Where are gems installed?
    • We can check using gem env, on the INSTALLATION DIRECTORY, and GEM PATHS are the paths where it’s loading all the gems of the current environment.
    • src: https://stackoverflow.com/a/3408910/3612053
    • It does not seem to be able to “install” in a local folder…
  • Difference between INSTALLATION DIRECTORY and USER INSTALLATION DIRECTORY:
# Generate a foobar.gem file
gem build
# Install a local gem globally
gem install foobar.gem
 
# Uninstall
gem uninstall foobar

Bundler

Bundler: The best way to manage a Ruby application’s gems

This tool allows you to manage your project dependencies and install exact gems and versions that are needed. These dependencies are specified in a Gemfile, which is placed in a project root directory.