<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Mcmire's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 18 May 2008 11:53:13 GMT</pubDate>
    <description>DZone Snippets: Mcmire's Code Snippets</description>
    <item>
      <title>Perform a Rails find() and iterate over the resulting records in groups</title>
      <link>http://snippets.dzone.com/posts/show/5461</link>
      <description>&lt;code&gt;&lt;br /&gt;module ActiveRecord&lt;br /&gt;  class Base&lt;br /&gt;    # This method lets you iterate over the results of a .find, in groups.&lt;br /&gt;    # (Basically an interface to LIMIT.)&lt;br /&gt;    # Anything you can pass as options to .find, you can pass here. &lt;br /&gt;    # Example 1:&lt;br /&gt;    #   Order.each_by(100, :conditions =&gt; { :cc_processed_at =&gt; nil }) do |order|&lt;br /&gt;    #     # do stuff with order&lt;br /&gt;    #   end&lt;br /&gt;    # Example 2:&lt;br /&gt;    #   Person.each_by(50, :order =&gt; 'name') do |person, index|&lt;br /&gt;    #     # do stuff with person and index&lt;br /&gt;    #   end&lt;br /&gt;    # Pass :update =&gt; true in the options to print a message before each group is&lt;br /&gt;    # fetched from the db.&lt;br /&gt;    #&lt;br /&gt;    # Author: Elliot Winkler &lt;elliot.winkler@gmail.com&gt;&lt;br /&gt;    # Source: http://snippets.dzone.com/posts/show/5461&lt;br /&gt;    def self.each_by(group_size, options={}, &amp;blk)&lt;br /&gt;      update = options.delete(:update) || false&lt;br /&gt;      num_records = count(options.except(:from))&lt;br /&gt;      return 0 if num_records == 0&lt;br /&gt;      #raise "Number of records: #{num_records}"&lt;br /&gt;      also_pass_offset = (blk.arity == 2)&lt;br /&gt;      0.step(num_records, group_size) do |offset|&lt;br /&gt;        find_options = { :offset =&gt; offset, :limit =&gt; group_size }.merge(options)&lt;br /&gt;        if update&lt;br /&gt;          if num_records == 1&lt;br /&gt;            puts "&gt;&gt; Reading the only record."&lt;br /&gt;          else&lt;br /&gt;            start_offset = offset + 1&lt;br /&gt;            end_offset   = offset + group_size&lt;br /&gt;            end_offset   = num_records if num_records &lt; end_offset&lt;br /&gt;            puts "&gt;&gt; Reading records #{start_offset}-#{end_offset}."&lt;br /&gt;          end&lt;br /&gt;        end &lt;br /&gt;        find(:all, find_options).each do |record|&lt;br /&gt;          also_pass_offset ? blk.call(record, offset) : blk.call(record)&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;      num_records&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 05 May 2008 15:35:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5461</guid>
      <author>mcmire ()</author>
    </item>
    <item>
      <title>howmany</title>
      <link>http://snippets.dzone.com/posts/show/5211</link>
      <description>&lt;code&gt;&lt;br /&gt;#!/usr/bin/perl&lt;br /&gt;#==========================================================================================&lt;br /&gt;# howmany -- a tool for determining how many different types of files are in a folder&lt;br /&gt;#------------------------------------------------------------------------------------------&lt;br /&gt;# Author: Elliot Winkler &lt;elliot.winkler@gmail.com&gt;&lt;br /&gt;# Created: 11 Mar 2008&lt;br /&gt;#==========================================================================================&lt;br /&gt;&lt;br /&gt;my $dir = $ARGV[0] || ".";&lt;br /&gt;my $cmd = "find $dir";&lt;br /&gt;my @listing = sort grep { $_ } split /\n/, `$cmd`;&lt;br /&gt;&lt;br /&gt;my %exts;&lt;br /&gt;for (@listing) {&lt;br /&gt;  my($ext) = /\.([a-z]+)$/;&lt;br /&gt;  next unless $ext;&lt;br /&gt;  $exts{lc $ext}++;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;for (sort keys %exts) {&lt;br /&gt;  print uc($_).": ".$exts{$_}."\n";&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;$ cd ~/docs&lt;br /&gt;$ howmany&lt;br /&gt;CSV: 3&lt;br /&gt;DOC: 1&lt;br /&gt;KEY: 1&lt;br /&gt;PUB: 1&lt;br /&gt;SQL: 1&lt;br /&gt;TEXT: 1&lt;br /&gt;TXT: 9&lt;br /&gt;XCF: 2&lt;br /&gt;XLS: 1&lt;br /&gt;ZIP: 1&lt;br /&gt;$ howmany ~/docs&lt;br /&gt;CSV: 3&lt;br /&gt;DOC: 1&lt;br /&gt;KEY: 1&lt;br /&gt;PUB: 1&lt;br /&gt;SQL: 1&lt;br /&gt;TEXT: 1&lt;br /&gt;TXT: 9&lt;br /&gt;XCF: 2&lt;br /&gt;XLS: 1&lt;br /&gt;ZIP: 1&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Note:&lt;/b&gt; This only works on Linux/Unix, because it relies on a Linux/Unix-only command to pull up the list of files. A future update may include support for Windows, though it would be pretty easy to find out how to fix it.</description>
      <pubDate>Tue, 11 Mar 2008 14:57:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5211</guid>
      <author>mcmire ()</author>
    </item>
  </channel>
</rss>
