Isolate Side Effects in Ruby
In this article, we’ll look at what side effects are, why they are problematic despite being necessary, and how to isolate them to minimise their drawbacks.
Related cards:
Change the current directory without side effects in Ruby
To temporarily change the current working directory in Ruby, call Dir.chdir
with a block. The previous working directory will be restored when the block ends:
Dir.chdir('/usr/local') do
# do stuff in /usr/local
end
Security issues with hash conditions in Rails 2 and Rails 3
Find conditions for scopes can be given either as an array (:conditions => ['state = ?', 'draft']
) or a hash (:conditions => { 'state' => 'draft' }
). The later is nicer to read, but has horrible security implications in some versions of Ru...
Webpacker: Side effects of using window.* within the ProvidePlugin
Some older Node modules rely on window.jQuery
to be present. One suggested solution is to use this config in the app/config/webpack/environment.js
:
const { environment...
Terminus: a client-side Capybara driver
Terminus is a Capybara driver where most of the driver functions are implemented in client-side JavaScript. It lets you script any browser on any machine using the Capybara API, without any browser plugins or extensions.
Heads up: Ruby implicitly converts a hash to keyword arguments
When a method has keyword arguments, Ruby offers implicit conversion of a Hash
argument into keyword arguments. This conversion is perfo...
Installing therubyracer and libv8 with Ruby 1.8 on OSX Mavericks
There seems to be no way to use therubyracer -v '0.11.4'
and libv8 -v '3.11.8.17'
on OS X Mavericks.
However, running bundle update therubyracer
worked for me. It installed therubyracer -v '0.12.1'
and libv8 -v '3.16.14.3'
and I had not...
Ruby 2.3.0 has a safe navigation operator
As announced before, Ruby has introduced a safe navigation operator with version 2.3.0. receiver&.method
prevents NoMethodError
s by intercepting method invoca...
Ruby: The YAML safe_load method hides some pitfalls
The Ruby standard lib ships with a YAML Parser called Psych. But serializing and deserializing data seems not as obvious as if you are using JSON.
To safely write and read YAML files you should use Psych#dump
(String#to_yaml
) and `Psych.safe...
What Ruby’s ||= (Double Pipe / Or Equals) Really Does
It is a common misunderstanding that all [op]=
-operators work the same way, but actually they don't.
||=
and &&=
Those are special cases, because the assignment will only happen if the first variable passes the check (false
or `ni...