Ruby: Flatten arrays by only one level
Array#flatten
Show archive.org snapshot
by default flattens an array recursively. To only flatten the array for e.g. one level, it takes an optional argument.
# Flattens the array recursively
>> [1, [2, [3]]].flatten
=> [1, 2, 3]
# Flattens the array the given number of times
>> [1, [2, [3]]].flatten(1)
=> [1, 2, [3]]
Related cards:
Git: Improve your commits by reviewing changes one-by-one
Git commits should be very deliberate, and only contain changes that you really want to be in there. In order to reduce the chance to accidentally commit something you didn't intend, review your changes before committing.
My preferred way of d...
Bundler in deploy mode shares gems between patch-level Ruby versions
A recent patch level Ruby update caused troubles to some of us as applications started to complain about incompatible gem versions. I'll try to explain how the...
Postgres: DISTINCT ON lets you select only one record per ordered attribute(s) for each group
-
To retrieve only unique combinations of the selected attributes: You can omit rows, where all selected columns are equal with the
DISTINCT
statement. - To retrieve the group wise maximum of certain columns: You can keep only one record fo...
Missing certificates for rubygems and bundler in Ruby 1.8.7
Using Ruby 1.8.7 you will not be able to use the maximum versions Rubygems 1.8.30 and Bundler 1.17.3 with https://rubygems.org/
anymore. This is a result of a server certificate on December 5th, 2020. The resulting errors will...
Fix Capistrano with RubyGems 1.6
After updating your RubyGems, you will probably not be able to run Capistrano any more, but receive an error similar to this:
can't activate net-ssh (= 2.0.22) for [], already activated net-ssh-2.1.0 for [] (Gem::LoadError)
If you have B...
The Ruby Object Model
In Ruby (almost) everything is an Object
. While this enables a lot of powerful features, this concept might be confusing for developers who have been programming in more static languages, such as Java or C#. This card should help understanding t...
Ruby on Rails Tutorial: Learn Rails by Example | by Michael Hartl
A thorough introduction to web development with Ruby on Rails
Install RubyMine under Ubuntu
This card explains how to install RubyMine for the first time. If you want to upgrade an existing RubyMine installation (after legacy install) to a newer version, see How to upgrade RubyMine.
Option A (new way)...
Tell RVM which patch level you mean by "1.8.7" or "1.9.3"
When you download or upgrade RVM it has a hardcoded notion which patch level it considers to be "1.9.3".
This can give you errors like "ruby-1.9.3-p392 is not installed"
even if you have another Ruby 1.9.3 that will do.
The solution is to defi...
Use byebug on Ruby 2+
The debugger
gem does not seem to be properly working on Ruby 2. Use byebug
instead!
Byebug is a simple to use, feature rich debugger for Ruby 2. It uses t...