I’ve moved!
This blog has moved to www.mrrubato.com.
Formatting source code in wordpress.com blogs
I just found out that there’s a way to format source code in wordpress.com blogs without having to mess around with the HTML yourself. You just need to surround your code with a tag.
From http://support.wordpress.com/code/:
Code in between the tags will automatically be encoded for display, you don’t need to worry about HTML entities or anything.
When I get the chance, I’ll go through the old entries and modify them to make use of this feature.
Overloading the ActiveRecord Setter
Sometimes, you want to do some extra work when you set an attribute on your ActiveRecord model instance.
You can’t just do this, because you’ll just end up recursively calling the very setter that you’re defining:
class Square < ActiveRecord::Base def side=(value) self.area = value*value self.side = value end end [/sourcecode] <div>...and you can't do this because you'll end up modifying a copy of the attributes hash of the model instance, which will not be written through to the database when the model instance is saved ( check out ActiveRecord::Base::attributes in active_record/base.rb):</div> class Square > ActiveRecord::Base def side=(value) self.attributes['side'] = value self.area = value * value end end
class Square > ActiveRecord::Base def side=(value) self.area = value * value # This would still work because we're calling the default setter for area write_attribute('side', value) end end
Ruby load path
There are three ways to refer to the variable that stores the load paths that Ruby checks for libraries when you use ‘require’ or ‘load.’
$:, $-I, $LOAD_PATH
19 Rails Tricks
I came across this article on Ruby Inside. It contains a nice quick list of things that you might not have known that you can do in Rails. I find the Rails Engines to be particularly interesting.
http://www.rubyinside.com/19-rails-tricks-most-rails-coders-dont-know-131.html
- Benchmark logic in your controller actions
- acts_as_nested_set
- Easier collections with to_proc
- Convert arrays to sentences in views
- Send files back to the user
- Iterating through page elements with RJS
- Check for existence
- Number helpers for common number tasks
- Testing different route configurations easily
- Get lots of info about requests
- Improving session performance even more than with ActiveRecord
- Caching unchanging data at application startup
- Check your views are rendering valid HTML / XHTML
- Cleaner HTML output testing
- Run long-running tasks separately in the background
- Make ids in URLs more user friendly
- Separate out slices of functionality into Engines
- Calculations
- XML or YAML output of your data
Which process is using that port?
A simple example: to find out which process is using TCP port 8080
$ fuser -n tcp 8080
Passing environment variables to sudo
You can pass environment variables such as $PYTHONPATH to sudo like this:
sudo env PYTHONPATH=$PYTHONPATH /some/python/script
How to delete a remote branch from a git repository
This isn’t obvious, so I thought I’d post an example for deleting a remote branch.
Let’s say you want to delete origin/some_branch from origin
$ git branch -a * master origin/master origin/some_branch
To remove the branch, just do this:
$ git push origin :some_branch
Now, it’s gone!
$ git branch -a * master origin/master
From the git push manual page:
The canonical format of a <refspec> parameter is +?<src>:<dst>; that is, an optional plus +, followed by the source ref, followed by a colon :, followed by the destination ref.
…Pushing an empty <src> allows you to delete the <dst> ref from the remote repository.
So, when you do “git push repo src:dest”, that translates to “git push <repository> <refspec>”. In the example above, <repository> was “origin” and <refspec> was “:some_branch”. Since there was no <src> part of the refspec, the <dst> ref was deleted from the remote repository.