Sometimes you just want to have a small web server that serves files to test something.
On Ruby 1.9.2+ you can do the following ("
." for current directory).
ruby -run -ehttpd . -p8000
Python 2.x offers a similar way.
python -m SimpleHTTPServer 8000 .
This is the same way with Python 3.x
python -m http.server
In both cases your web server is single-threaded and will block when large files are being downloaded from you.
WEBrick also offers [a simple way](https://stackoverflow.com/quest…
When using Sidekiq in your application, you must write thread-safe code.
This wiki page also lists gems that are known to be unsafe on threaded applications.
When adding a gem that will also be used by a Sidekiq worker, make sure to confirm it's thread-safe.
When you need to create a locale for a language variant (like Austrian for German), you probably don't want to duplicate your entire
de.yml file only to change a few minor exceptions for our Austrian friends.
Luckily, the I18n gem used by Rails has a fallback feature where you can make one locale file fall back to another if no translation is available.
In the example above you would have a
de_DE: # hundreds of translations here
… and another …
git show branch:file. Examples:
git show HEAD~:bin/command git show origin/master:../lib/version.rb
For newer Ubuntu versions we currently need to install the patch level version 1.8.7-p375, otherwise the dev dependencies from
openssl will cause the installation to fail.
For a project that specifies the Ruby version
1.8.7 in the
.ruby-version the rbenv autoswitch will not work. You have several options how you can solve this problem:
This card shows basic techniques for fixing a flaky integration test suite that sometimes passes and sometimes fails. "Integration test" is a test script that remote-controls a web browser with tools like Selenium WebDriver.
Although the examples in this card use Cucumber and Selenium, the techniques are applicable to all languages and testing tools.
Your tests probably look like this:
When I click on A When I click on B When I click on C Then I should see effects of C
A test like this works fine most of t…
I had an issue with icons from an icon font aligning differently on Linux, iOS and Windows (seemingly browser-independent). With
vertical-align:middle, they aligned properly on Linux, iOS and macOS, whereas with a
-18%, it looked good on Windows and iOS, but not Linux.
Further investigation showed that not only icons, but also normal capital letters aligned differently. No setting of
vertical-align could fix this, neither
middle, nor additional paddings or margins. It seems like browsers take the…
> 9112347935156469760 9112347935156470000
So according to IEEE floating point definition only numbers between
-(2^53 - 1) (-9007199254740991) and
Note that ECMAScript 6 will probably also offer [
When an Rspec example fails, I usually investigate by running that example again using
rspec <file:line>. However, this does not work with shared examples, since Rspec doesn't know in which context the shared example should be run.
But there is a different way: You can run the shared example using the
--example option. It takes a string value and runs all scenarios containing that substring in their full description.
This allows you to run a single uniquely named example, all examples with
similar names, all the examples in a u…
This is a small example on how you can check if your Postgres index can be used by a specific query in you Rails application. For more complex execution plans it might still be a good idea to use the same path of proof.
puts User.order(:last_name, :created_at).to_sql # => SELECT "users".* FROM "users" ORDER BY "users"."last_name" ASC, "users"."created_at" ASC
add_index :users, [:last_name, :created_at]
Web applications can be used by multiple users at the same time. A typical application server like Passenger has multiple worker processes for a single app. In a distributed deployment setup like we use at makandra you will even have multiple application servers, each with their own worker pool.
This means that your code needs to deal with concurrent data access. The two main tools we use to cope with concurrency are database transactions and distributed locks. These two are not interchangeable. You ca…
In Rspec 3 the DSL like
describe is exposed globally by default. Therefore it is not necessary to write
However, there is a config option to disable this beavior, which also disables the old
RSpec.configure do |config| config.disable_monkey_patching! end
In order to still use
Rspec, you can set
expose_dsl_globally = true after
RSpec.configure do |con…
All columns of a model's database table are automagically available through accessors on the Active Record object.
When you need to specialize this behavior, you may override the default accessors (using the same name as the attribute) and simply call the original implementation with a modified value. Example:
class Poet < ApplicationRecord def name=(value) super(value.strip) end end
Note that you can also avoid the original setter and directly read/write from/to the instance's attribute storage. However th…
When installing gems, a lot of time is spent building locally installed documentation that you probably never use.
We recommend you disable documentation generation for
gem install by default.
Note that Bundler won't install documentation, so this advice applies only when installing gems manually.
If you don't already have it, create a
~/.gemrc file. The gemrc is a Yaml file, so add the following line to add default switches to the
(If you do n…
When a Rails controller action should handle both HTML and JSON responses, do not use
request.xhr? to decide that. Use
I've too often seen code like this:
def show # ... if request.xhr? render json: @user.as_json else # renders default HTML view end end
This is just plain wrong. Web browsers often fetch JSON via XHR, but they (should) also send the correct
Accept HTTP header to tell the server the data they expect to receive.
If you say
request.xhr? as a means for "…
You login to a Linux server with a performance issue: what do you check in the first minute?
uptime dmesg | tail vmstat 1 mpstat -P ALL 1 pidstat 1 iostat -xz 1 free -m sar -n DEV 1 sar -n TCP,ETCP 1 top
When MySQL refuses to use your index, you can do a number of things wrong. One of them might be conditions with improper data types.
For example, let's assume you have a
users table with an
varchar) which is indexed.
MySQL will use the index when your query is well-formed:
mysql> EXPLAIN SELECT * FROM users WHERE email = 'email@example.com'; +----+-------------+-------+-------+----------------------+----------------------+---------+-------+------+-------+ | id | select_type | table | type | poss...
For rastered image formats like JPG or PNG, each pixel is basically drawn on a fixed size canvas. To display such an image in a different size (say: 1.5 times larger than original), the renderer (your Browser / GPU / Monitor) needs to interpolate the color values of missing pixels. The image will appear slightly blurred.
This is different for vector graphics like the SVG (Scalable Vector Graphics) format. You can imagine SVG files as XML file contai…