Skip to content

git-add-ing lines by line numbers

Every now and then I want have control over staging my files by line number. While `git add -p` lets you add hunks of code as split by git, and you can continue trying to use the ‘s’ command in the interactive console to try splitting it, it still doesn’t work as I would like to split things up.

At such times I use git-gui and really like it.



I just came across git-gui

It is a minimalistic graphical user interface that comes with git. It’s cool


Array#map necessarily works on all elements.

If you only want to pick certain elements from an array use Array#take_while

a = [1, 2, 3, 4, 5, 0]
a.take_while { |i| i < 3 }  
#=> [1, 2]

However, it seems that it doesn’t always work. Should be a bug:

2.0.0-p247 :098 > a.class
=> Array
2.0.0-p247 :099 > a[62].class
=> String
2.0.0-p247 :100 > a[62]
=> "1-7"
2.0.0-p247 :096 > a[62].scan(/\D/)
=> ["-"]
2.0.0-p247 :097 > a[62].scan(/\D/).any?
=> true
2.0.0-p247 :101 > a.take_while{|h| h.scan(/\D/).any? } 
=> []

 NOTE: A good alternative for Array#take_while might be Array#keep_if

using psql to run sql queries in console

use the -c option for entering the query
If you want the output to be printed without it being interactive (or using a buffer?) redirect the out put to /dev/null (i.e. use < /dev/null at the end of the command)

$psql -d 'my_db' -c 'select * from my_table' < /dev/null

rails 4 heroku deploy

If you are deploying a rails 4 app to heroku and hit errors during `assset:precompile` or a bit after that because your environment variables aren’t getting picked up correctly, try:

`heroku labs:enable user-env-compile`

Using a module instead of a class

Sometimes I write code in a class which I feel might be better written in a module. I don’t initialize the class and have a hard time imagining it as a object. It mostly houses procedural code.

I found this SO link which gives a good answer:

also this might be useful :

Ruby Dir.glob ** pattern

I like to autoload all files in my app/model folder in my Rails app.

“config.autoload_paths accepts an array of paths from which Rails will autoload constants. Default is all directories under app.”

– Rails Guides

Now ruby docs for Dir.glob suggest using ‘**’ to match directories recursively. However, I notice that ‘**’ does not match any more or less files than ‘*’.

> Dir[Rails.root.join('app', 'models', '*')] == Dir[Rails.root.join('app', 'models', '**')]
 => true 

From this stack overflow post I got the idea of using ‘**/’ instead and that seems to work.

I am creating my simple cheatsheet here:

‘*’ – matches all files and directories in the current path
‘**’ – matches all files and directories in the current path
‘**/’ – matches all directories recursively in the current path
‘**/*’ – matches all files and directories recursively in the current path