Quickly printing data in columns on your Ruby console
Dump this method into your Ruby console to quickly print data in columns. This is helpful for e.g. comparing attributes of a set of Rails records.
def tp(objects, *method_names)
terminal_width = `tput cols`.to_i
cols = objects.count + 1 # Label column
col_width = (terminal_width / cols) - 1 # Column spacing
Array(method_names).map do |method_name|
cells = objects.map{ |o| o.send(method_name).inspect }
cells.unshift(method_name)
puts cells.map{ |cell| cell.to_s.ljust(col_width) }.join ' '
end
nil
end
Usag...
Git: undo delete
Assuming you're wanting to undo the effects of git rm
or rm
followed by git add -A
or something similar:
This restores the file status in the index:
git reset -- <file>
then check out a copy from the index
git checkout -- <file>
To undo git add
, the first line above suffices, assuming you haven't committed yet.
Note:
Make sure to use double dashes --
to tell git to checkout a file instead of a branch. This only is relevant for [files having the same name as a branch](https://git-scm.com/docs/git-ch...
Howto: Require a gem that is not in Gemfile
In case you want to require a gem, that is not in the Gemfile of you bundle and therefore not in your loadpath, you need to add the path manually and require the gem afterwards.
Expected error
Requiring a gem, that is not in the Gemfile
or .gemspec
, will cause an LoadError
exception:
require 'example_gem' => LoadError: cannot load such file -- example_gem
Adding a gem to the loadpath temporary
- You need to install the
gem
gem install 'example_gem'
- Then you need to require the path where the gem was install...
Classic CSS problems that are easy with flexbox
Solved with flexbox is a collection of css problems which were hard or impossible to solve without flexbox:
- Better, Simpler Grid Systems
- Holy Grail Layout
- Input Add-ons
- Media Object
- Sticky Footer
- Vertical Centering
Rubymine provides a visual merge conflict resolution tool
RubyMine provides a visual tool for resolving merge conflicts locally.
Follow
Git > Resolve Conflicts
in the context menu to open RubyMine's merge conflict tool.
- Left pane: local copy (read-only)
- Right pane: checked in version from repository (read-only)
- Central pane: base revision from which both conflicting versions are derived
You can also use a similar pane view to compare to files.
Mark two files and press Ctrl + D
to compare.
How to use your iPhone's internet connection on your Ubuntu machine via USB
Luckily, this is simple. Just install three packages:
sudo apt install ipheth-utils libimobiledevice-dev libimobiledevice-utils
Then turn on the "Personal Hotspot" in iPhone settings, connect it to your Ubuntu machine via USB and you should be up and running.
Solved: Element inside overflow:scroll container cannot be scrolled on iOS when dragged by a contained iframe
Imagine the following HTML structure, where the scrolling container has overflow-y: scroll
:
+--scrolling container+-+
| |
| +-child element+----+ |
| | ++iframe++ | |
| | | | | |
| | | | | |
+-----------------------+
| | | | <-- actually cut off by
| +--------+ | <-- scrolling container
+-------------------+
The issue: On iOS, the child element cannot be scrolled when grabbing the iframe (i.e. putting your finger somewhere on the iframe).
...
Rendering 404s for missing images via Rails routes
When you load a dump for development, records may reference images that are not available on your machine.
Requests to those images may end up on your application, e.g. if a catch-all route is defined that leads to a controller doing some heavy lifting. On pages with lots of missing images, this slows down development response times.
You can fix that by defining a Rails route like this:
if Rails.env.development?
scope format: true, constraints: { format: /jpg|png|gif/ } do
get '/*anything', to: proc { [404, {}, ['']] }
...
How to disable Chrome's save password bubble for Selenium tests
When filling out forms in Selenium tests, Chrome shows the (usual) bubble, asking to store those credentials.
While the bubble does not interfere with tests, it is annoying when debugging tests. Here are two ways to disable it:
Option 1: prefs
You can set profile preferences to disable the password manager like so:
prefs = {
'credentials_enable_service' => false,
'profile.password_manager_enabled' => false
}
Capybara::Selenium::Driver.new(app, browser: :chrome, prefs: prefs)
Sadly, there are no command line s...
image-to-DataURI converter: Duri.me
Small web application where you can upload an image (PNG, JPEG, GIF) and generate a base64-encoded version of it.
You can copy the result as
- HTML
<img>
tag with data URI, - CSS rule with
background-image
and data URI, - plain Base64-encoded data URI string.
How to make http/https requests yourself with nc/telnet/openssl
Sometimes you want/have to send specific http(s) requests. You can do that easy with curl
or just write the request yourself.
make a http request with nc
nc example.com 80
GET / HTTP/1.1
Host: example.com
# press enter
make a http request with telnet
telnet example.com 80
GET / HTTP/1.1
Host: example.com
# press enter
make https request with openssl
openssl s_client -connect example.com:443
GET / HTTP/1.1
Host: example.com
# press enter
You can specify more headers if you want:
nc example.c...
Sending errors to sentry from development
For the initial setup or changes in the sentry reporting it might be useful to enabled reporting of sentry in development. Don't commit these changes and prefer to report to the staging environment. As other developers might be confused of these errors try to given them a proper message and delete them afterwards.
- Add
config.raven_dsn = 'your-dns'
inconfig/environments/development.rb
. - Add development to existing environments in the
Raven.configure
block:config.environments = ['development', 'staging', 'production']
. - ...
How to view Image Metadata on the Linux Command Line with ImageMagick
ImageMagick has a command line tool called identify
which can read image metadata:
>identify -verbose DSC00136.JPG
Image: DSC00136.JPG
Format: JPEG (Joint Photographic Experts Group JFIF format)
Class: DirectClass
Geometry: 5472x3648+0+0
Resolution: 350x350
Print size: 15.6343x10.4229
Units: PixelsPerInch
Type: TrueColor
Endianess: Undefined
Colorspace: sRGB
Depth: 8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
Channel statistics:
Red:
min: 0 (0)
max: 255 (1)
mean: 11...
iOS Safari scales text in landscape mode
iOS Safari tries to be helpful and enlarges some(!) texts when you turn to landscape mode. In precise CSS building, this is annoying. Disable this behavior with:
body
-webkit-text-size-adjust: 100% // Prevent font scaling in iOS landscape
Vim read-only option
You can start vim
with a read-only option. This prevents making accidentally changes in files you don't want to touch.
view /file/to/open
view
is actually vim
.
Use case
You have opened many similar files and accidentally type :wq
in the wrong one. Did you make changes? Which changes you made? When do you notice you edited the wrong file?
Beware: Nested Spreewald patiently blocks are not patient
Note: The behaviour of Spreewald's within
step is as described below for version < 1.9.0; For Spreewald >= 1.9.0 it is as described in Solution 1.
When doing integration testing with cucumber and selenium you will often encounter problems with timing - For example if your test runs faster than your application, html elements may not yet be visible when the test looks for them. That's why Spreewald (a collection of cucumber steps) has a concept of doing things patiently
, which means a given b...
Generating barcodes with the Barby gem
Barby is a great Ruby gem to generate barcodes of all different sorts.
It includes support for QR codes via rQRCode; if you need to render only QR codes, you may want to use that directly.
Example usage
Generating a barcode is simple:
>> Barby::Code128.new('Hello Universe').to_png
=> "\x89PNG\r\n\u001A..."
Configuration
Barby supports several barcode types and you must require all necessary files explicitly.
For the example a...
Auto-squashing Git Commits
git
command line options for automating common rebasing tasks, like adding a fix to a commit that was already rebased into the history.
When does support end for a version of Internet Explorer?
The maximum version of Internet Explorer you can have depends on your version of Windows. E.g. Windows 7 users can use Internet Explorer up to version 11, but they cannot upgrade to Edge.
Since early 2016, Microsoft only supports the latest version of IE that is available for a given version of Windows. If you are using an older version of IE than you could, you will no longer receive security patches for your version.
In April 2017, this means the following:
- Intern...
Rails: Default generators
This is a visualization of the files that will be generated by some useful rails generators. Invoke a generator from command line via rails generate GENERATOR [args] [options]
. List all generators (including rails generators) with rails g -h
.
generator | model | migration | controller | entry in routes.rb
|
views | tests |
---|---|---|---|---|---|---|
scaffold | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
resource | ✔ | ✔ | ✔ ... |
JavaScript Coordinates
To move elements around we should be familiar with coordinates. Most JavaScript methods deal with one of two coordinate systems:
- Relative to the window(or another viewport) top/left.
- Relative to the document top/left.
It’s important to understand the difference and which type is where.
SASS: Defining linear sizes
Just dumping this in case somebody might need it.
When you need a CSS value (a padding, margin, height etc) to shrink/grow proportionally with the parent element, you normally use percentage values. However, if you need specific values at two given widths, you need to turn to linear functions. The mixin below gives you just that.
// Call with two desired values at two different widths.
// Returns a calc() expression that will scale proportionally between those two.
// Example:
// Spaci...
How to show an ordered crontab
Crontabs are often unordered, especially when generated for an application where you usually group tasks by their domain/scope.
An example crontab might look like this:
# Begin Whenever generated tasks for: project100
MAILTO="log@example.com"
MAILFROM="cron@example.com"
# When server is booting up, ensure Sidekiq is running
@reboot start_sidekiq
23 8 * * * baz
30 * * * * plop
5 8 * * * bar
1 0 * * * foo
# End Whenever generated tasks for: project100
While you can human-parse this one easily, crontabs with several lines are hard ...