Ruby Findings

digging for gems

Dot quad mask to CIDR

Posted by Art Green Sat, 22 Sep 2007 05:01:00 GMT

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 | no comments |

Textmate plugin for rails

Posted by Bill Marquette Fri, 14 Sep 2007 20:41:00 GMT

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.

TextMate links

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/footnotes

Posted in | no comments |

Object destruction

Posted by Art Green Fri, 07 Sep 2007 17:48:00 GMT

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_collect

Posted in | no comments |

Multiple class initialization methods

Posted by Art Green Thu, 06 Sep 2007 01:18:00 GMT

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 , | no comments |

tabnav rails plugin

Posted by Bill Marquette Wed, 05 Sep 2007 21:26:00 GMT

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 | no comments |

Subversion setup rake task for rails

Posted by Bill Marquette Wed, 05 Sep 2007 21:16:00 GMT

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 | no comments |

Vlad the Deployer

Posted by Bill Marquette Wed, 05 Sep 2007 20:59:00 GMT

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 | no comments |

Older posts: 1 2


html>