PostgreSQL and its way of sorting strings
PostgreSQL uses the C library's locale facilities for sorting strings:
- First, all the letters are compared, ignoring spaces and punctuation.
- It sorts upper and lower case letters together. So the order will be something like
a A b B c C
- Then, spaces and punctuation are compared to break ties.
Example:
Ruby | PostgreSQL |
---|---|
IMAGE3.jpg | image2.jpg |
image.jpg | image3.jpg |
image2.jpg | IMAGE3.jpg |
image3.jpg | image.jpg |
Further reading
- [PostgreSQL-FAQ: Why do my strings sort incorrectly?](h...
Ruby: Converting UTF-8 codepoints to characters
Converting string characters to or from their integer value (7-bit ASCII value or UTF-8 codepoint) can be done in different ways in Ruby:
-
String#ord
orString#unpack
to get character values -
Integer#chr
orArray#pack
to convert character values into Strings
Character values to Strings
Integer#chr
To get the character for a 7-bit ASCII value or UTF-8 codepoint (0-127) you can use Integer#chr
:
116.chr
# => "t"
To get a character for values larger than 127, you need to pass the encoding. E.g. to get codepoint 25...
How to organize monkey patches in Ruby on Rails projects
As your Rails project grows, you will accumulate a number of small patches. These will usually fix a bug in a gem, or add a method to core classes.
Instead of putting many files into config/initializers
, I recommend to group them by gem in lib/ext
:
lib/
ext/
factory_girl/
mixin.rb
carrierwave/
change_storage.rb
fix_cache_ids.rb
sanitize_filename_characters.rb
ruby/
range/
covers_range.rb
array/
dump_to_excel.rb
xss_aware_join.rb
enumerable/
...
Introducing Helix: Rust + Ruby, Without The Glue
Helix allows you to implement performance-critical code of your Ruby app in Rust, without requiring glue code to bridge between both languages.
See attached article for a longer write-up about the why and how.
How to Work With Time Zones in Rails
When dealing with time zones in Rails, there is one key fact to keep in mind:
Rails has configurable time zones, while
Ruby is always in the server's time zone
Thus, using Ruby's time API will give you wrong results for different time zones.
"Without" time zones
You can not actually disable time zones, because their existence is a fact. You can, however, tell Rails the only single time zone you'll need is the server's.
config.time_zone = "Berlin" # Local time zone
config.active_record.default_timezone = :loca...
rroblak/seed_dump
This gem gives you a rake task db:seed:dump
do create a db/seeds.rb
from your current database state.
The generated db/seeds.rb
will look this:
Product.create!([
{ category_id: 1, description: "Long Sleeve Shirt", name: "Long Sleeve Shirt" },
{ category_id: 3, description: "Plain White Tee Shirt", name: "Plain T-Shirt" }
])
User.create!([
{ password: "123456", username: "test_1" },
{ password: "234567", username: "test_2" }
])
Install MySQL 5.6 in Ubuntu 16.04
Instead of using this hack you might want to use MariaDB 10.x which can work with both old and new apps.
An alternative could be to use the MySQL Docker image which is still updated for 5.6.
Ubuntu 16.04 only provides packages for MySQL 5.7 which has a range of backwards compatibility issues with code written against older MySQL versions.
Oracle maintains a list of official APT repositories for MySQL 5.6, but those repositories do...
Using Bumbler to Reduce Runtime Dependencies - The Lean Software Boutique
Tool to show you which gems are slow to load:
➜ git:(master) ✗ bundle exec bumbler
[################################################# ]
(49/65) travis-lint...
Slow requires:
110.21 render_anywhere
147.33 nokogiri
173.83 haml
179.62 sass-rails
205.04 delayed_job_active_record
286.76 rails
289.36 mail
291.98 capistrano
326.05 delayed_job
414.27 pry
852.13 salesforce_bulk_api
Stackprof - sampling call-stack profiler for ruby
Stackprof is a sampling call-stack profile for Ruby 2.1+.
Instead of tracking all method calls, it will simply collect the current stack trace of a running program at fixed intervals. Methods that appear on top of the stack trace most often, are the methods your program spends most of its time in.
The big advantage of this is that it is very fast. You can even enable it in production and collect real performance data. See the README on how to add it as a middleware. It will dump its data to the tmp
directory.
Sampling is by default base...
djberg96/sys-filesystem: A Ruby library for getting filesystem information
Uses FFI and works all relevant operating systems.
If you'd try to do it yourself, you'd have to use FFI which is a bit awkward (see the gem's code), or call commands like df -B1
and search the strings for your relevant data.
That gem takes the pain away and works nicely.
Testing ActiveRecord validations with RSpec
Validations should be covered by a model's spec.
This card shows how to test an individual validation. This is preferrable to save an entire record and see whether it is invalid.
Recipe for testing any validation
In general any validation test for an attribute :attribute_being_tested
looks like this:
- Make a model instance (named
record
below) - Run validations by saying
record.validate
- Check if
record.errors[:attribute_being_tested]
contains the expected validation error - Put the attribute into a valid state
- Run...
Bundler: Install gems behind a proxy
To install gems Bundler needs to be able to talk to https://api.rubygems.org
.
If you are behind a proxy you can use the https_proxy
environment variable:
https_proxy=http://myproxy:123 bundle install
Note that if there is no https_proxy
env variable, Bundler will also look for a http_proxy
env variable.
With Capistrano
Ideally the server you're deploying on exports an https_proxy
variable for all shells.
If you don't have control over the server setup, you can also add this to your Capistrano config:
...
NoMethodError: undefined method `cache' for Gem:Module
I got this error when running Rails 2.3 tests for Rails LTS. More stacktrace:
NoMethodError: undefined method `cache' for Gem:Module
/vagrant/rails-2-3-lts-repository/railties/lib/rails_generator/lookup.rb:212:in `each'
/vagrant/rails-2-3-lts-repository/railties/lib/rails_generator/lookup.rb:146:in `to_a'
/vagrant/rails-2-3-lts-repository/railties/lib/rails_generator/lookup.rb:146:in `cache'
/opt/vagrant_ruby/lib/ruby/1.8/fileutils.rb:243:in `inject'
/vagrant/rails-2-3-lts-repository/railties/l...
Showing a custom maintenance page while deploying
Note
The maintenance mode is enabled on all application server as soon as the file
/public/system/maintenance.html
is present.
Installation
Add this line to your application's Gemfile:
gem 'capistrano', '~> 3.0'
gem 'capistrano-maintenance', '~> 1.0'
Add this line to you application's Capfile:
require 'capistrano/maintenance'
Enable task
Present a maintenance page to visitors. Disables your application's web interface by writing a #{maintenance_basename}.html
file to each web server. The servers m...
Linux Performance Analysis in 60,000 Milliseconds
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
Also see:
rack-mini-profiler - the Secret Weapon of Ruby and Rails Speed
rack-mini-profiler is a powerful Swiss army knife for Rack app performance. Measure SQL queries, memory allocation and CPU time.
This should probably not be loaded in production (the article recommends otherwise), but this looks like a useful tool.
Unindent HEREDOCs in Ruby 2.3
In Ruby 2.3 you can use <<~
instead of <<-
to automatically remove indentation from a HEREDOCs:
str = <<~MESSAGE
Hello Universe!
This is me.
Bye!
MESSAGE
str
will now be:
Hello Universe!
This is me.
Bye!
Make Nokogiri use system libxml2
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 to update their pre-built binaries and release a new version of the gem. Then, you need to update and ...
How to encode or decode quoted-printable strings
E-mails are usually encoded using Quoted Printable. Here is how to decode or encode such strings.
You probably know Quoted Printable from e-mail bodies appearing in Rails logs, where =
s become =3D
s in URLs, or where long lines are split up and trailed by =
after each split.
Decode Quoted Printable
Decoding such strings is actually quite simple using plain Ruby:
"foo=3Dbar".unpack('M')[0]
# => "foo=bar"
Note that unpack
will return an array. Our result is the 1st item.
...
Stop using bundle exec
Install this gem and stop using bundle exec
or even Geordi's handy b
. Yay!
Sidekiq: Problems and Troubleshooting
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.
Beware ruby's var1 = var2 = "value" multiple assignment
This looks like it is safe to use:
2.2.1 :001 > a = b = "hello world"
"hello world"
2.2.1 :002 > a
"hello world"
2.2.1 :003 > b
"hello world"
2.2.1 :004 > b = " goodbye!"
" goodbye!"
2.2.1 :005 > a
"hello world"
2.2.1 :006 > b
" goodbye!"
But it isn't!
2.2.1 :010 > a = b = "hello world"
"hello world"
2.2.1 :011 > a
"hello world"
2.2.1 :012 > b
"hello world"
2.2.1 :013 > b << " goodbye!"
"hello world goodbye!"
2.2.1 :014 > a
"hello world goodbye!"
2.2.1 :015 > b
"hello world goodbye!"
What is happening when we do `a = b = ...
Upgrade from Ruby 1.8.7 to 2.1.5 – an incomplete guide
I recommend to go straight to 2.1.5+
without intermediate steps. Otherwhise you burden yourself with unnecessary work of encoding problems.
Issues you may encounter:
- Set the ruby version within your
.ruby-version
file to2.1.5
- Remove gem
ruby-debug
and use e.g.byebug
- Remove gem
oniguruma
- Remove gem
fastercsv
- Replace gem
mysql
withmysql2
- Update gem capistrano
2.12.0
to~>2.12
when bound forRuby 1.8.7
and remove obsolete explicite Gemfile entries fornet-scp
andnet-ssh
if present. - Update gem `and...
Override Cucumber steps without an ambiguity error
Cucumber raises a Cucumber::Ambiguous
if more than one step definitions match a step.
Our new cucumber_priority gem provides a way to mark step definitions as overridable, meaning that they can always be overshadowed by a more specific version without raising an error.
This gem is currently used by spreewald and cucumber_factory.