Search_spinner Cross_grey Magnifier
help

Search syntax

  • Search for repeating cards with repeats:yes
  • Search for unfinished cards with unfinished:yes
  • Search for cards in a certain topic with topic:"name"
  • Search for phrases with "a phrase"
  • Exclude a word from search: -word
  • Exclude a phrase from search: -"a phrase"
  • Exclude a topic from search: -topic:"name"

Git: Push a new branch and track it immediately

When you create a new branch and push it to origin, you won't be tracking it. This means a git pull won't know its remote version.

You could use difficult commands to set up a branch's tracking but it's easier to just push it like this:

git push -u

From the documentation on git push:

-u, --set-upstream \
For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands. For more information, see branch.<…

Make your regular expressions more readable

The following two hints are taken from Github's Ruby style guide:

If your regular expression mentions a lot of forward slashes, you can use %r(...), %r[...] or %r{...} instead of /.../.

 %r(/blog/2011/(.*))
 %r{/blog/2011/(.*)}
 %r[/blog/2011/(.*)]

If your regular expression is growing complex, you can use the x modifier to ignore whitespace and comments:

regexp = %r{
  start         # some text
  \s            # white space char
  (group)       # first group    ...
External content

KeyboardJS

KeyboardJS is a library for binding to keys or key combos

Hack-fix Selenium::WebDriver::Element#select is deprecated

In some older Capybara versions (e.g. 0.3.9), we're getting lots of deprecations warnings:

Selenium::WebDriver::Element#select is deprecated. Please use Selenium::WebDriver::Element#click and determine the current state with Selenium::WebDriver::Element#selected?

Hani-elabed on Github helps. Add this code to features/support/env.rb to remove them temporarily:

```
# June 30th, 2011
# a temporary hack to disable the annoying upstream warnings capybara > selenium-webdriver 0.2.2
# capybara folks know about this and are working on it. S…

MySQL shell: Enable UTF-8

When you do a script/dbconsole -p, your MySQL shell will already be using UTF-8. When you call it yourself using mysql, it may not be enabled.

You'll notice that when you get ASCII salad and/or question marks instead of special characters. \
Example: Hlavn� m?sto Praha instead of Hlavní město Praha.

You need to manually switch on UTF-8, in the MySQL console:

SET NAMES 'utf8';

Define a route that only responds to a specific format

You won't usually have to do this. It's OK to route all formats to a controller, and let the controller decide to which format it wants to respond.

Should you for some reason want to define a route that only responds to a given format, here is how you do it:

Rails 3

match 'sitemap.xml' => 'feeds#sitemap', :constraints => { :format => 'xml' }, :as => 'sitemap'

Rails 2

map.sitemap 'sitemap.xml', :controller => 'feeds', :action => 'sitemap', :format => 'xml'
External content

Tabs in Textarea Plugin for jQuery

This is a demo of the "Tabby" Javascript jQuery plugin to use tabs in regular textareas to make them suitable for in-browser coding of languages like HTML, CSS, Javascript, or your favorite server-side language. The idea is to be able to use a press of the TAB button or SHIFT+TAB to indent or outdent your code.

Show MySQL process list without sleeping connections

Usually you don't need to, but when you want to see which queries your MySQL server currently needs to handle (and if there are locks, etc), you could say SHOW PROCESSLIST in a MySQL shell.

Unfortunately, SHOW PROCESSLIST does not allow filtering. When you are on MySQL ≥ 5.1.7, do this instead:

SELECT * FROM information_schema.processlist WHERE command != 'Sleep' ORDER BY id;

That also allows you to only show some values or order differently, like so:

SELECT user, time, state, info FROM information_schema.processlist WHERE co...

VirtualBox host IP address and hostname

When you are using NAT in your virtual machine (which you should), the host's IP address is:

10.0.2.2

You'll need it to access shared folders or your host's web server when testing pages in IE.

Fun fact: You could also use vbox.srv – that's the corresponding hostname.

Fix [RubyODBC]Cannot allocate SQLHENV when connecting to MSSQL 2005 with Ruby 1.8.7. on Ubuntu 10.10

I followed this nice guide Connecting to MSSQL with Ruby on Ubuntu - lambie.org until I ran in the following errors:

irb(main):001:0> require "dbi"; dbh = DBI.connect('dbi:ODBC:MyLegacyServer', 'my_name', 'my_password')

DBI::DatabaseError: INTERN (0) [RubyODBC]Cannot allocate SQLHENV
  from /usr/lib/ruby/1.8/dbd/odbc/driver.rb:36:in `connect'
  from /usr/lib/ruby/1.8/dbi/handles/driver.rb:33:in `connect'
  from /usr/lib/ruby...
External content

Render Single-Line Markdown Text with Redcarpet

We love Markdown. We use it wherever we can for text formatting. In a web app, the obvious place for it is in large text areas, where we can allow complete freedom of formatting. Headers, paragraphs, lists, it’s all good.

What about the formatting of text in single-line text fields? If our form entry is a single line, that’s usually how its text will be displayed in our interface. In this case, we probably want to avoid all the block-level elements that Markdown will let the user create.

MySQL shell: Vertical vs horizontal layout

When talking to your MySQL server via a mysql shell, you can terminate queries by ; or \G – the latter gives you a vertical output.

You know this:

mysql> SELECT * FROM users;
+----+---------+---------------------+-----------------+
| id | name    | email               | greeting        |
+----+---------+---------------------+-----------------+
|  1 | Alice   | alice@example.com   | Hello world!    |
|  2 | Bob     | bob@example.com     | Hello universe! |
|  3 | Charlie | charlie@example.com | Hi mom!    ...

Ruby: Find the most common string from an array

This will give you the string that appears most often in an array:

names = %w[ foo foo bar bar bar baz ]
names.group_by(&:to_s).values.max_by(&:size).try(:first)
=> "bar"

This is very similar to the linked StackOverflow thread, but does not break on empty arrays.

Note that try is provided by ActiveSupport (Rails). You could explicitly load activesupport or use andand on plain Ruby.

Change how Capybara sees or ignores hidden elements

Short version

  • Capybara has a global option (Capybara.ignore_hidden_elements) that determines whether Capybara sees or ignores hidden elements.
  • Prefer not to change this global option, and use the :visible option when calling page.find(...). This way the behavior is only changed for this one find and your step doesn't have confusing side effects.
  • Every Capybara driver has its own notion of "visibility".

Long version

Capybara has an option (Capybara.ignore_hidden_elements) to configure the default…

Customize path for Capybara "show me the page" files

When you regularly make use of Cucumber's "show me the page" step (or let pages pop up as errors occur), the capybara-20120326132013.html files will clutter up your Rails root directory.

To tell Capybara where it should save those files instead, put this into features/support/env.rb:

Capybara.save_and_open_page_path = 'tmp/capybara'

Linux: Mount second encrypted HDD automatically without entering a password

This is one possibility to do this. There are other and maybe even better ways to do this.

  1. Generate a key for your encrypted harddisk:

    dd if=/dev/random of=/home/bob/keyfile_sdb1 bs=4096 count=1
    
  2. Then add your keyfile to encrypted harddisk: How to change your dm-crypt passphrase (step 3)

  3. Create a mountpoint:

    mkdir /mnt/space
    
  4. Create a script e.g. in your homedirectory (/home/bob/mount_sdb1.sh):

    #!bin/bash
       
    ...
    
External content

Ruby, Ruby on Rails, and _why: The disappearance of one of the world’s most beloved computer programmers

Nice article to educate your non-geek girlfriend/boyfriend about the joys of programming.

3019 cards