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 }createmethod 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::Validationis a gem that validates the inputsinclude AnotherClasskeyword to get all the methods fromAnotherClassextend AnotherClasskeyword will import all the static methods fromAnotherClass
def self.method== static VSdef methodspecific to an instancepryinstruction 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:NilClasserror).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 folderArgumentErroris the generic error to use instead of using a customInvalidArgumentErrorexceptioninclude Validatedis 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 PATHSare 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 DIRECTORYandUSER 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-installflag, 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 foobarBundler
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.