This card is a short summary on different ways of assigning multiple attributes to an instance of a class. Using positional parameters Using parameters is the default when assigning attributes...

...It works good for a small number of attributes, but becomes more difficult to read when using multiple attributes. Example: class User def initialize(salutation, first_name, last_name, street...

...Adding Records via XHR and JS Example For the following examples we use a simple data model where a user has zero or more tasks. class ExampleMigration < ActiveRecord::Migration...

...user.tasks.build } end def update load_user @user.attributes = user_params if @user.save flash[:notice] = 'User saved successfully.' redirect_to(edit_variant_1_user_path(@user)) else flash[:notice] = 'User could not...

A JavaScript error in an E2E test with Selenium will not cause your test to fail. This may cause you to miss errors in your frontend code. Using the BrowserConsole...

!!driver_logs_proc end def driver_logs_proc browser = page.driver.browser if browser.respond_to?(:logs) # selenium-webdriver >= 4 proc { browser.logs } elsif browser.respond_to?(:manage) && browser.manage.respond_to?(:logs) # selenium-webdriver...

...always is to prevent long-running queries in the first place, automatic timeouts can serve as a safety net to terminate problematic queries automatically if a set time limit is...

...statement due to statement timeout (PG::QueryCanceled)). If multiple SQL statements appear in a single simple-query message, the timeout is applied to each statement separately. Set the timeout globally...

While it might seem trivial to implement an invoice that sums up items and shows net, gross and vat totals, it actually involves a lot of rules and caveats. It...

...examples in Ruby and MySQL, the concepts apply to all programming languages and data stores. When to round There are exactly two spots where your invoice logic should round money...

If you want to switch to another ruby versions, you have several options, depending on what you want: Do you want to switch temporarily, per project, or globally?

Unlike RVM, rbenv does not offer a command like rvm use. By default, it respects your project's .ruby-version file. If you need to change manually...

When you find similar groups of expect calls in your tests, you can improve readability by extracting the group into its own matcher. RSpec makes this easy by allowing matchers...

We can extract the repeated matcher chains into a custom matcher called be_shouting: expect(foo).to be_shouting expect(bar).to be_shouting Instead of re-implementing the...

...issues, depending on the exact version, and depending on your app. However, it is still possible to give some generic advice on how you want to tackle the update in...

...really confident about upgrading Rails, have a look at Rails LTS. How many update steps? Besides the Rails upgrade itself, you might also want to upgrade your other gems and...

If you need to convert an SVG source to PS or EPS, the most common suggestion on the interwebs is to use Inkscape from the commandline. Inkscape is a fairly...

...converting is CairoSVG. CairoSVG is available on most Linux distros through their package management systems, e.g. apt install cairosvg on Ubuntu. It has few dependencies (most importantly Python 3 and...

The nokogiri gem provides different packages for several platforms. Each platform-specific variant ships pre-built binaries of libxml2, e.g. x86_64-linux includes binaries for 64bit Linux on Intel/AMD...

...This significantly speeds up installation of the gem, as Nokogiri no longer needs to compile libxml2. However, this also means that for each security issue with libxml2, Nokogiri maintainers have...

Because your examples should not change global state, you should not need to care about the order in which RSpec processes your .rb files. However, in some cases you might...

...rb files in alphabetical order of their file paths by default (or when you specify --order defined). You run tests in random order by using --order random on the command...

stackoverflow.com

When you use method_missing to have an object return something on a method call, always make sure you also redefine respond_to_missing?. If you don't do it...

def method_missing(method_name, *args, &block) if method_name == :bark 'woof!' else super end end end This will allow you to say: Dog.new.bark => "woof!" But: Dog.new.respond_to? :bark...

Why string sorting sucks in vanilla Ruby Ruby's sort method doesn't work as expected with special characters (like German umlauts): ["Schwertner", "Schöler"].sort # => ["Schwertner", "Schöler"] # you probably expected...

...Schöler", "Schwertner"] Also numbers in strings will be sorted character by character which you probably don't want: ["1", "2", "11"].sort # => ["1", "11", "2"] # you probably expected...

When you have an Angular directive that transcludes content, you might want to do something in case there is no content inside your element, like showing some default content.

...you can not do something like default goes here . Angular will always empty that element's text, even if there is nothing to transclude. But you can use your directive...

Jasmine has long standing support for writing asynchronous specs. In days gone by we used the done callback to achieve this, but these days it is possible to write much...

...more readable specs. Async specs As a first example, say we want to check that some form disables the submit button while working. // bad (how we used to do it...

You have uncommited changes (you can always check by using git status), which you want to discard. Context Now there are several options to discard these depending on...

...your exact situation. The headlines will differentiate the cases whether the files are staged or unstaged. Staged and unstaged changes Staged changes Unstaged Changes Staged and unstaged changes

...tough challenge. To get more detailed insights consider using the rack-mini-profiler gem. Setup with Unpoly Add the following gems: group :development do gem 'memory_profiler' gem 'rack-mini...

If you have slow views (Haml/Partials can be slow), consider caching them. Remove code & sql-queries that are not needed to render the page. Calling to_a blindly on...

...have 2 HTML boxes. The first one has a margin-bottom of let's say 30px and the second one a margin-top of 20px. After rules of collapsing margins...

...too. For example child elements with margins also collapse with margins of the previous sibling of the parent box. Nevertheless there are some exceptions where the behavior of vertical collapsing...

linux.die.net

...files. Those can be used to apply to a different repository [1] or by someone else (e.g. sent when sent to them via e-mail). Creating a patch in git...

...changes and commit them. Run git format-patch COMMIT_REFERENCE to convert all commits since the referenced commit (not including it) into patch files. For example, let's say you...

...core and available in all ruby versions without the need to install anything. This serializes complete ruby objects including id, object_id and all internal state. Marshal.load deserializes a string...

...to an object. A deserialized object cannot be saved to database directly as the the dumped object was not marked dirty, thus rails does not see the need to save...

I frequently find myself needing a combination of group_by, count and sort for quick statistics. Here's a method on Enumerable that combines the three: module Enumerable

group_by(&block) .transform_values(&:count) .sort_by(&:last) .to_h end end Just paste that snippet into a Rails console and use #count_by now! Usage examples...

...to read the The framework field guide - Fundamentals, the first of a three part series to learn the basics of frontend technologies. I can highly suggest it for learning the...

...fundamentals. 'The framework field guide' is written by Unicron Utterances and there side has many high quality articles on web development and computer science related to programming. The Framework Field...

...pretty_print method As an example, consider the following class. class MyClass # ... def inspect "#<#{self.class} attr1: #{attr1.inspect}, attr2: #{attr2.inspect}>" end end Instances of that class will inspect like #<MyClass attr1...

...Alice", attr2: "Bob">, but IRB will apply a single color (green) for everything. That is because MyClass implements only inspect. If it were to implement pretty_print, IRB would use...

When ending a Selenium test Capybara resets the browser state by closing the tab, clearing cookies, localStorage, etc. It may be a good idea to wait for all in-flight...

...AJAX requests to finish before ending a scenario: You may have client-side JavaScript that freaks out when the tab closure kills their pending requests. If that JavaScript opens an...