<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: ruby code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 29 Aug 2008 18:23:52 GMT</pubDate>
    <description>DZone Snippets: ruby code</description>
    <item>
      <title>ActiveRecord each_by_page</title>
      <link>http://snippets.dzone.com/posts/show/3557</link>
      <description>Perform an operation on a set of models including ActiveRecord callbacks, without instantiating all models at once.&lt;br /&gt;&lt;br /&gt;Step through and instantiate each member of the class and execute on it, but instantiate no more than per_page instances at any given time.&lt;br /&gt;Safe for destructive actions or actions that modify the fields your :order or :conditions clauses operate on.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;module ActiveRecord&lt;br /&gt;  class Base&lt;br /&gt;    def each_by_page per_page, options = {}, &amp;block&lt;br /&gt;      # By-id for model-modifying blocks&lt;br /&gt;      # Build SQL to get ids of all matching records using the options provided by the user&lt;br /&gt;      sql = construct_finder_sql(options.dup.merge({ :select =&gt; 'id' }))&lt;br /&gt;      # Get the results as an array of tiny hashes { "id" =&gt; "1" } and flatten them out to just the ids&lt;br /&gt;      all_ids = connection.select_all(sql).map { |h| h['id'] }&lt;br /&gt;      at_a_time = 0..(per_page-1)&lt;br /&gt; &lt;br /&gt;      # chop apart the all_ids array a segment at a time&lt;br /&gt;      begin&lt;br /&gt;        ids = all_ids.slice!(at_a_time)&lt;br /&gt;        ids_cases = []&lt;br /&gt;        ids.each_with_index { |id, i| ids_cases &lt;&lt; "WHEN #{id} THEN #{i}" }&lt;br /&gt;        ids_cases = ids_cases.join(' ')&lt;br /&gt; &lt;br /&gt;        # Do the deed on this page of results&lt;br /&gt;        find(:all, options.merge(&lt;br /&gt;          :conditions =&gt; [ 'id IN (?)', ids ],&lt;br /&gt;          :order =&gt; "CASE id #{ids_cases} END"&lt;br /&gt;        )).each &amp;block&lt;br /&gt; &lt;br /&gt;      end until all_ids.empty?&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.dweebd.com/ruby/activerecord-pagination-for-destructive-migrations/#comment-3"&gt;link to blog entry&lt;/a&gt;</description>
      <pubDate>Tue, 20 Feb 2007 23:59:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3557</guid>
      <author>duncanbeevers (Duncan Beevers)</author>
    </item>
    <item>
      <title>Split String into roughly equal-sized chunks.</title>
      <link>http://snippets.dzone.com/posts/show/2631</link>
      <description>Split a string into an array of roughly equal sized chunks based on a string or regular expression delimiter.&lt;br /&gt;Delimiter is preserved in output.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class String&lt;br /&gt;  def chunk_string(average_segment_size = 40, sclice_on = /\s+/)&lt;br /&gt;    out = []&lt;br /&gt;    slices_estimate = self.size.divmod(average_segment_size)&lt;br /&gt;    slice_count = (slices_estimate[1] &gt; 0 ? slices_estimate[0] + 1 : slices_estimate[0])&lt;br /&gt;    slice_guess = self.size / slice_count&lt;br /&gt;    previous_slice_location = 0&lt;br /&gt;    (1..slice_count - 1).each do&lt;br /&gt;      |i|&lt;br /&gt;      slice_location = self.nearest_split(slice_guess * i, sclice_on)&lt;br /&gt;      out &lt;&lt; self.slice(previous_slice_location..slice_location)&lt;br /&gt;      previous_slice_location = slice_location + 1&lt;br /&gt;    end&lt;br /&gt;    out &lt;&lt; self.slice(previous_slice_location..self.size)&lt;br /&gt;    out&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def nearest_split(slice_start, slice_on)&lt;br /&gt;    left_scan_location  = (self.slice(0..slice_start).rindex(slice_on)).to_i&lt;br /&gt;    right_scan_location = (self.slice((slice_start+1)..self.size).index(slice_on)).to_i + slice_start&lt;br /&gt;    ((slice_start - left_scan_location) &lt; (right_scan_location - slice_start) ? left_scan_location : right_scan_location)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 21 Sep 2006 00:29:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2631</guid>
      <author>duncanbeevers (Duncan Beevers)</author>
    </item>
    <item>
      <title>Return the extension from a file name</title>
      <link>http://snippets.dzone.com/posts/show/2555</link>
      <description>// Return the extension from a file name&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/(.*\.)(.*$)/.match(File.basename(file_name))[2]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 07 Sep 2006 02:06:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2555</guid>
      <author>duncanbeevers (Duncan Beevers)</author>
    </item>
  </channel>
</rss>
