View

How to enable the Thinkpad microphone mute key on Ubuntu 16.04

While the hardware mute button of my Lenovo x230 worked on Ubuntu 14.04 out of the box, it does not on Ubuntu 16.04. It is fairly simple to fix, though.

There is an extensive answer on Ask Ubuntu, but only part of it was required for me. Here is the gist of it.

  1. Open a terminal

  2. Run acpi_listen and press the mute key. You should see something like this:

    button/f20 F20 00000080 00000000 K
    

    Press Ctrl+C to exit.

  3. Run amixer scontrols. You will see multiple lines, one of which sh…

How to change the class in FactoryGirl traits

FactoryGirl allows a :class option to its factory definitions, to set the class to construct. However, this option is not supported for traits.

Most often, you can just define a nested factory instead of a trait, and use the :class option there.

factory :message do
  factory :reply, class: Message::Reply do
    # ...
  end
end

If you need/want to use traits instead (for example, it might make more sense semantically), you can not use a :class on a trait.

In that case, use initialize_with to define the record's const…

Auto-destruct in 11 days

rbenv: Upgrade Rubygems for all installed Rubies

This will upgrade Rubygems for all installed Ruby versions. It will also honor the maximum Rubygems version compatible with Rails 2.3.

for i in `rbenv versions --bare`; do rbenv shell $i; echo $i; if [ ${i:0:4} = 1.8. ]; then gem update --system 1.8.30; else gem update --system; fi; done
Repeats

Dealing with I18n::InvalidPluralizationData errors

When localizing model attributes via I18n you may run into errors like this:

I18n::InvalidPluralizationData: translation data {...} can not be used with :count => 1. key 'one' is missing.

They seem to appear out of the blue and the error message is more confusing than helpful.

The problem

When you run into this, you probably introduced a new model that has attributes like these:

class User < ApplicationRecord
  has_many :posts
end

```
class Post < ApplicationRecord
belongs_to :user
validates :user, presence: true …

Repeats

Ruby: A small summary of what return, break and next means for blocks

Summary

  • Use return to return from a method. return accepts a value that will be the return value of the method call.
  • Use break to quit from a block and from the method that yielded to the block. break accepts a value that supplies the result of the expression it is “breaking” out of.
  • Use next to skip the rest of the current iteration. next accepts an argument that will be the result of that block iteration.

The following method will serve as an example in the details below:

```
def example(&block)
puts block.cal…

How to pair a Bose Quiet Comfort 35 with your Ubuntu computer

You need to disable "Bluetooth low energy", then follow these core steps:

1) Make sure the headphones are in pairing mode.
2) Pair with System Settings > Bluetooth
3) Select & test the headphones in System Settings > Sound. Choose High Fidelity Playback (A2DP Sink).

I also had to install a package with sudo apt-get install pulseaudio-module-bluetooth and load it with pactl load-module module-bluetooth-discover. Put the latter command into ~/.bashrc or you'll need to run it after each boot.

Resources

  • <https://askubuntu.com/a/83…
Auto-destruct in 15 days

Updated: Carrierwave – Recreate versions

Added hint that Carrierwave's recreate_versions! will only ever process :Symbol arguments (i.e. not strings).

Repeats

How to avoid ActiveRecord::EnvironmentMismatchError on "rails db:drop"

After loading a staging dump into development, you might get an ActiveRecord::EnvironmentMismatchError when trying to replace the database (like rails db:drop, rails db:schema:load).

$ rails db:drop
rails aborted!
ActiveRecord::EnvironmentMismatchError: You are attempting to modify a database that was last run in `staging` environment.
You are running in `development` environment. If you are sure you want to continue, first set the environment using:

        bin/rails db:environment:set RAILS_ENV=development

Starting with R…

Ubuntu MATE: Fixing ALT + TAB being really slow

On my Ubuntu MATE machine, switching applications with ALT + TAB was impossible, because it took nearly 2 seconds. The culprit appears to be an "UX improvement" gone bad: MATE renders a live screenshot of each running application inside the application switcher.

Fixing this is simple:

  • Open "Window Preferences"
  • Either uncheck "Enable software compositing window manager" or check "Disable thumbnails in Alt-Tab"
Repeats

In MySQL, a zero number equals any string

In MySQL comparing zero to a string 0 = "any string" is always true!

So when you want to compare a string with a value of an integer column, you have to cast your integer value into a string like follows:

SELECT * from posts WHERE CAST(posts.comments_count AS CHAR) = '200' 

Of course this is usually not what you want to use for selecting your data as this might cause some expensive database operations. No indexes can be used and a full table scan will always be triggered.

If possible, cast the compared value in your application to…

Auto-destruct in 9 days

Updated: List of Helpful RubyMine Shortcuts

Added how to diff two files: Select both in the project pane, then press CTRL + D. That easy!

JavaScript bookmarklet to click an element and copy its text contents

Here is some JavaScript code that allows you to click the screen and get the clicked element's text contents (or value, in case of inputs).

The approach is simple: we place an overlay so you don't really click the target element. When you click the overlay, we look up the element underneath it and show its text in a browser dialog. You can then copy it from there.

It will also highlight the clicked element.

Here is the one-liner URL that you can store as a bookmark. Place it in your bookmarks bar and click it to activate.

```
javascript:…

Rails: render a template that accepts a block by using the layout option of render

Let's say you have a form that you render a few times but you would like to customize your submit section each time. You can achieve this by rendering your form partial as layout and passing in a block. Your template or partial then serves as the surrounding layout of the block that you pass in. You can then yield back the form to the block and access the form in your block.

record/_form.haml

= form_for record do |form|
  ...
  .form-actions
    yield(form)
  

In order to make your template record/_form.haml accept a block whe…

Import Excel files without running into memory limitations

There are several gems that make it easy to read and process xlsx files. Parsing the entire file at once however is error-prone since each cell is transformed to a ruby object - sometimes including thousands of formatted but empty cells.

As of today, I found two promising alternatives that provide a stream-based access to spradsheet rows:

  • Roo supports multiple spreadsheet types like ods or csv and has a quite large contributor base
  • [Creek](https://github.com/partpilotl…

Sentry: How to get a list of events that is programmatically processable

The linked documentation explains how to get a JSON list of the latest 100 events of a given issue. Simply open this URL while logged in:

https://sentry.io/api/0/issues/<issue id>/events/
Repeats

How to send HTTP requests using cURL

  • Reading a URL via GET:

    curl http://example.com/
    
  • Defining any HTTP method (like POST or PUT):

    curl http://example.com/users/1 -XPUT
    
  • Sending data with a request:

    curl http://example.com/users -d"first_name=Bruce&last_name=Wayne"
    

    If you use -d and do not set an HTTP request method it automatically defaults to POST.

  • Performing basic authentication:

    curl http://user:password@example.com/users/1
    
  • All together now:

    curl http://user:password@example.com/users/1 -XPUT -d"screen_name=batman"
    

Postgres: How to force database sessions to terminate

If another session is accessing your database you are trying to reset or drop you might have seen the following error:

PG::ObjectInUse: ERROR:  database "foo_development" is being accessed by other users
DETAIL:  There is 1 other session using the database.

This could be the rails server, rubymine and many more. Beside terminating the session connection manually you can also find out the pid and kill the process.

1. rails db
2. SELECT * FROM pg_stat_activity;

```
datid | 98359
datname | foo_developm…

Repeats

Using regular expressions in JavaScript

Regular expressions in Javascript are represented by a RegExp object. There also is a regex literal as in many other languages: /regex/. However, they are used slightly differently.

Regex literal

  • Usage: /foo+/
  • Shorthand for creating a regular expression object

RegExp() object

  • Usage: Regexp("foo+") or new RegExp("foo+")
  • No surrounding slashes required (they're the literal markers)
  • Since the argument is a string, backslashes need to be escaped as well: RegExp("\\d+")

Gotchas

  • Regex objects [never eq…
View
3390 cards