Treating gems as plugins
This is specific to rails 1.2 as I believe Edge Rails has this built in.
There’s been some amount of debate on various blogs on whether packaging up gems in your Rails application is a good choice. In my case, I decided that for me, the deployment convenience (a single application on a single server) far outweighs the maintenance aspects.
I found the ‘gems’ plugin over at techno-weenie.net that does exactly what I want with little effort. It’s a standard script/plugin install and installs a rake task (rake gems:freeze) which takes an argument of the gem you want to freeze. From the README
$ rake gems:freeze GEM=tzinfoinstalls the tzinfo gem in vendor/tzinfo-x.y.z (where x.y.z is the version of tzinfo that was frozen). If needed, you can freeze a specific gem version with:
$ rake gems:freeze GEM=tzinfo VERSION=0.1.2Posted in Rails | no comments |
Dot quad mask to CIDR
def dqtocidr( mask )
bitcount = 0
mask.split('.').each { |octet|
o = octet.to_i
bitcount += (o & 1) and (o >>= 1) until o == 0
}
return bitcount
end
irb(main):016:0> dqtocidr(‘128.0.0.0’) => 1
irb(main):017:0> dqtocidr(‘192.0.0.0’) => 2
irb(main):018:0> dqtocidr(‘224.0.0.0’) => 3
irb(main):019:0> dqtocidr(‘240.0.0.0’) => 4
irb(main):020:0> dqtocidr(‘248.0.0.0’) => 5
irb(main):021:0> dqtocidr(‘252.0.0.0’) => 6
irb(main):022:0> dqtocidr(‘254.0.0.0’) => 7
irb(main):023:0> dqtocidr(‘255.0.0.0’) => 8
Posted in Ruby | no comments |
Textmate plugin for rails
I’m trying to make more and better use out of TextMate for my rails development and have been bitching about having to find the code a rails backtrace points to when my apps blow up. Enter the footnotes plugin. This plugin will give you some links in the footer of your page that allow you to go directly to the controller, view, layout, stylesheets, javascripts, etc in TextMate. It also hyperlinks your rails backtraces so you can go direct to the line of code that’s erroring out.
Note the underlined lines in this backtrace - those are hyperlinks.
To install the plugin just run this command from your RAILS_ROOT.
script/plugin install http://macromates.com/svn/Bundles/trunk/Bundles/Ruby%20on%20Rails.tmbundle/Support/plugins/footnotesPosted in Rails | no comments |
Object destruction
Ruby does not provide programmer defined destruction methods. In C++ you can define a method that is messaged when the object instance is destroyed. This method can perform clean-up operations such as deleting temporary files.
Ruby, however, destroys objects dynamically during a scheduled garbage collection. This means that, during a run, it is impossible to know when an object instance will be destroyed. This fact combined with the lack of a destruction method can create headaches when there is a need to clean-up during object destruction.
Since there is no direct support for a destructor method, you must call a custom function, or more specific a proc object, when the garbage collector is about to destroy the object. As stated before it is unpredictable when this occurs.
Also if such a finalizer object has a reference to the orignal object, this may prevent the original object to get garbage collected.
Because of this problem, the finalize method below is a class method and not a instance method. So if you need to free resources for an object, like closing a socket or killing a spawned subprocess, you should do it explicitly with the ObjectSpace class.
class MyClass
def initialize
ObjectSpace.define_finalizer(self,
self.class.method(:finalize).to_proc)
end
def MyClass.finalize(id)
puts "Object #{id} dying at #{Time.new}"
end
end
# test code
3.times {
MyClass.new
}
ObjectSpace.garbage_collectPosted in Ruby | no comments |
Multiple class initialization methods
Here’s a quick run-down on the development of multiple initialize methods for Ruby classes: Multiple Initialize Methods.
The author looks at the subject from a domain language idiom perspective. I used the information to provide multiple creation interfaces for a class.
Quick example:
Class Archive
def Archive.create( archive_name )
return self.new( archive_name, :create )
end
def Archive.open( archive_name )
return self.new( archive_name, :open )
end
def initialize( archive_name, mode )
# do interesting stuff
end
end
arch = Archive.open("some_existing_file")
arch = Archive.create("some_new_file")
Posted in Ruby, Rails | no comments |
tabnav rails plugin
While working on a rails app a couple days ago, I found myself needing to implement a tabbed interface. I hacked it together, but meant to go back to it later and clean it up. I stumbled upon the tabnav plugin in the meantime. Haven’t had the time to try it out, but based on reading it’s examples and a few blog entries here and there about it, I think it’ll solve my problem very nicely.
Posted in Rails | no comments |
Subversion setup rake task for rails
Found this nice little rake task to setup your rails app in a new subversion repository over at DZone Snippets the other day.
Posted in Rails | no comments |
Vlad the Deployer
Vlad the Deployer is a rails deployment tool built to be simpler than Capistrano. Having spent many moons trying to figure out Capistrano, I’m all for checking out a replacement.
Posted in Rails | no comments |
