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 fromengines/online_identity/test/factories/master_patient.rb
, must come from a Gemfactory_bot
- The fields are created using
test/factories/appointment.rb
- The fields are created using
pundit_user
: access control using pundit gem- some tests checks all controllers must have
pundit_user
, that’s why we set an empty object
- some tests checks all controllers must have
Dry::Validation
is a gem that validates the inputsinclude AnotherClass
keyword to get all the methods fromAnotherClass
extend AnotherClass
keyword will import all the static methods fromAnotherClass
def self.method
== static VSdef method
specific to an instancepry
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 benil
(Avoiding anundefined method for nil:NilClass
error).method(&:symbol)
is like static method call in java, .e.gOptional::get
::Namespace
→ tell ruby to go to the root folder, otherwise, it will think it’s in the same folderArgumentError
is the generic error to use instead of using a customInvalidArgumentError
exceptioninclude Validated
is used more on active records “world”- rails initializer
- use stdout flush to print directly
Resources
Tutorials
- Ruby Essentials - Techotopia
- Learn Ruby with the Edgecase Ruby Koans
- RubyMonk - Interactive Ruby tutorials
- DNS points to prohibited IP | tryruby.org | Cloudflare
Convention
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 theINSTALLATION DIRECTORY
, andGEM 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…
- We can check using
- Difference between
INSTALLATION DIRECTORY
andUSER INSTALLATION DIRECTORY
:- The User Installation Directory (which has always existed) variable for RubyGems is the directory where gems will be installed when using the
--user-install
flag, which is a directory immediately within your home directory. - src: ruby on rails - What is “USER INSTALLATION DIRECTORY” in rubyGems env - Stack Overflow
- The User Installation Directory (which has always existed) variable for RubyGems is the directory where gems will be installed when using the
# 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.