Ruby: Don't a add return in ensure
This method won't throw an error:
def a_method
raise
ensure
return :something
end
it will in fact return :something
So please proceed with care :)
Related cards:
Caching: don't use content_for, it won't work
Do not use content_for
inside a cached view fragment. It won't work because Memcache will just output whatever is in the cache, and not execute such commands.
Add indexes on foreign keys when you create a migration (with foreign key)
Whenever you make a migration to add a foreign key, you should also add an index for it
def self.up
add_column :comments, :user_id, :integer
add_index :comments, :user_id
end
Profiling Ruby with ruby-prof
require 'ruby-prof'
# you don't need this if you have ruby-prof in your Gemfile
You can set one of the things you want to measure by using (default is PROCESS_TIME
most useful ones are PROCESS_TIME, MEMORY, CPU_TIME):
RubyProf.me...
Method return value should always be of same type
One of the main source of bugs and complexity in the code is when a functional method (that we expect to return a value) return different values under different circumstances.
For example, we ruby programmers have a bad habit of returning nil fro...
Creating a gem in lib folder
Go to lib folder and use bundler to generate main files for a gem:
$ bundle gem test_gem
create test_gem/Gemfile
create test_gem/Rakefile
create test_gem/LICENSE
create test_gem/README.md
...
Find out branches containing a commit
If you have a commit and you want to see in what branches is is included, you have to write this:
git branch -r --contains [COMMIT-SHA]
-r is for remote
Rebase your feature branches
Regularly, but at least before merging your feature branches, rebase them to get rid of "fix typo" and "wip" commits.
Getting rid of unnecessary commits
Let's say you do a git rebase -i HEAD~~~~
and have this...
Database: Scopes, migrations, and indices
Wether you modify an existing named scope or add a new one, or when you write a new query, make sure you have the proper indices.
This particularly applies if you're going to run non-trivial queries of course (admin backends, analytics, etc).
##...
Killing wkhtmltopdf during cucumber
wkhtmltopdf
hangs on mac during cucumber unless we click on it. The main reason is with the version we use which is 0.11.0_rc1 and in out app/bin we have another version and it is a known issue with these versions. The fix is to go to 0.9.9, to ...
Respect time zones: Use Time.current and Time.zone.parse
Reading the current time
Don't use Time.now
as it's broken when using time zones. \
Instead, use Time.current
, DateTime.current
or Date.current
...