Blog Entries tagged "til"

Enable Spell Checking in Vim for Markdown and Git Commit Messages

I had a ridiculous typo in a Git commit message recently, so I decided to explore spell checking in Vim. As it turns out, it’s extremely easy.

I only wanted it enabled for Git commit messages and markdown, so I added the following to my vimrc / init.vim:

" Spell-check Markdown files and Git Commit Messages
autocmd FileType markdown setlocal spell
autocmd FileType gitcommit setlocal spell

By doing this, it highlights potential misspellings in red underline. You can see a list of potential corrections by moving the cursor over the word in normal mode and...

Read More...

Get the Previous Expression Value in IEx

When using Elixir, I’ve long missed the special _ helper from irb that returns the result of the previous expression. Sometimes I actually type it out of pure habit.

Little did I know that Elixir has the same thing, except better.

IEx.Helpers has a function v(n \\ -1) that returns the value of the nth expression in the session history. So v alone returns the previous expression and v(-2) returns the one before it.

iex(1)> "abc"
iex(2)> "zyx"
iex(3)> v
"zyx"
iex(4)> v(-3)
"abc"

Ruby Symbol and String Array Shorthand

Ruby has some handy Array shorthands, but I always forget which is which, so I thought I should write them down.

Symbol Array Shorthand:

%i{foo bar}  # => [:foo, :bar]

String Array Shorthand:

%w{foo bar}  # => ["foo", "bar"]

Both also work with square brackets %w[a b c] or parenthesis %i(a b c)

SSH Tunnels into Production Rails Database

Today I needed to access a production database for my Rails app directly using a GUI application on my mac, so I figured out that this can be done by creating an SSH tunnel like so:

ssh -Ng -L <local-port>:<remote-host>:<remote-port> <user>@<remote-host>
ssh -Ng -L 3307:100.64.26.11:3307 adam@100.64.26.11

And then, in your Rails app, update the database.yml as if you were connecting to a local database, but specify the proper database name.

development:
  adapter: mysql2
  database: myapp_production
  host: 127.0.0.1
  port: 3307
  username: root
  password

Read More...

Rails presence method

I use Rails’ present? method constantly, but recently I stumbled across the presence method.

In the past, I have often found myself doing the following because params[:foo] could be nil or a blank string.

foo = params[:foo].present? ? params[:foo] : "something else"

I hate that i have to write params[:foo] twice. But the presence method is handy for this case. It returns nil if the item is nil or a blank string, but otherwise returns the value.

foo = params[:foo].presence || "something-else"

TIL: Pass in an ActiveRecord Object to a where string

I’m surprised I didn’t already know this, but if you have an ActiveRecord where statement that references an id column, you can just pass in an object and it will pass in the object’s id, not the string representation of the object.

company = Company.find(1)

User.where("users.company_id = ?", company)
# is equal to
User.where("users.company_id = ?", company.id)

So when you create named scopes, you don’t have to do the song-and-dance where you check to see if the object is a model or an integer, as shown below.

scope :by_company, lambda { |company|
 ...

Read More...

Lookup IP Address & Location via the Command Line

With ifconfig.co, you can lookup your external IP Address and Location Information using a simple curl command. You can use it like so:

$ curl ifconfig.co/json
{
  "ip": "96.27.202.202",
  "ip_decimal": 1612409628,
  "country": "United States",
  "country_iso": "US",
  "city": "Columbus",
  "hostname": "d27-96-101-101.nap.wideopenwest.com"
}

$ curl ifconfig.co/city
Columbus

$ curl ifconfig.co/country
United States