<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: rails code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 17:26:21 GMT</pubDate>
    <description>DZone Snippets: rails code</description>
    <item>
      <title>Matrix rotator</title>
      <link>http://snippets.dzone.com/posts/show/3404</link>
      <description>This method rotates a matrix&lt;br /&gt;Example output:&lt;br /&gt;~/Desktop% ruby rotate.rb&lt;br /&gt;normal&lt;br /&gt;12345&lt;br /&gt;00000&lt;br /&gt;fooba&lt;br /&gt;rotated left&lt;br /&gt;50a&lt;br /&gt;40b&lt;br /&gt;30o&lt;br /&gt;20o&lt;br /&gt;10f&lt;br /&gt;rotated right&lt;br /&gt;f01&lt;br /&gt;o02&lt;br /&gt;o03&lt;br /&gt;b04&lt;br /&gt;a05&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def rotateMatrix(matrix, direction)&lt;br /&gt;  # - You must Rotate the matrix neo!&lt;br /&gt;  oldMap = matrix&lt;br /&gt;&lt;br /&gt;  # Get the number of lines in the old map (they're the new columns)&lt;br /&gt;  lineCount = oldMap.size&lt;br /&gt;  # Get the number of columns in the old map (We have that many rows now)&lt;br /&gt;  columnCount = oldMap[0].size&lt;br /&gt;  @map = []&lt;br /&gt;  columnCount.times { @map.push [] }&lt;br /&gt;&lt;br /&gt;  # Loop through every line in the old map, retrieve the appropriate column&lt;br /&gt;  # and make a horizontal column with it's contents&lt;br /&gt;  # we'll take one (old)line at a time and rotate it.&lt;br /&gt;  onLine = 0&lt;br /&gt;  oldMap.each do |oldLine|&lt;br /&gt;    onColumn = 0&lt;br /&gt;    oldLine.each do&lt;br /&gt;      case direction&lt;br /&gt;      when :right&lt;br /&gt;        @map[(columnCount - 1) - onColumn][(lineCount - 1) - onLine] = oldLine[(columnCount - 1) - onColumn]&lt;br /&gt;      when :left&lt;br /&gt;        @map[onColumn][onLine] = oldLine[(columnCount - 1) - onColumn]&lt;br /&gt;      end&lt;br /&gt;      onColumn += 1&lt;br /&gt;    end&lt;br /&gt;    onLine += 1&lt;br /&gt;  end&lt;br /&gt;  @map&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def rotateRight(matrix)&lt;br /&gt;  rotateMatrix(matrix, :right)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def rotateLeft(matrix)&lt;br /&gt;  rotateMatrix(matrix, :left)&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 02 Feb 2007 01:07:05 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3404</guid>
      <author>aptiva (Fj&#195;&#182;lnir &#195;?sgeirsson)</author>
    </item>
    <item>
      <title>File icon giver/getter/whatever</title>
      <link>http://snippets.dzone.com/posts/show/2560</link>
      <description>This code uses acts_as_attachment (that's the attachment.filename) but you can of course replace it with anything.&lt;br /&gt;It gets the file extension then looks up that extension in a dir full of files named extension.png&lt;br /&gt;if none is found, unknown.png is used&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;%&lt;br /&gt;extension = File.extname(attachment.filename).gsub(/\./, "")&lt;br /&gt;# get a file icon&lt;br /&gt;icon_path = nil&lt;br /&gt;Dir.entries("#{RAILS_ROOT}/public/images/filetypes").each do |entry|&lt;br /&gt;	entry_extension = File.extname(entry)&lt;br /&gt;	if entry.gsub(entry_extension, "") === extension&lt;br /&gt;		icon_path = "/images/filetypes/" + entry&lt;br /&gt;		break&lt;br /&gt;	end&lt;br /&gt;end&lt;br /&gt;icon_path = "/images/filetypes/unknown.png" if icon_path.nil?&lt;br /&gt; %&gt;&lt;br /&gt;&lt;%= image_tag icon_path %&gt;&lt;br /&gt;&lt;span class="attachment_name"&gt;&lt;%= attachment.filename %&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 07 Sep 2006 19:32:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2560</guid>
      <author>aptiva (Fj&#195;&#182;lnir &#195;?sgeirsson)</author>
    </item>
    <item>
      <title>all_children!</title>
      <link>http://snippets.dzone.com/posts/show/2523</link>
      <description>This snippet just gives you all the children of a model that acts_as_tree&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def all_children&lt;br /&gt;    Page.all_children_for self&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def self.all_children_for(parent, arr = [])&lt;br /&gt;    parent.children.each { |child| arr.push child }&lt;br /&gt;    parent.children.each { |child| all_children_for child, arr }&lt;br /&gt;    arr&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 03 Sep 2006 16:11:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2523</guid>
      <author>aptiva (Fj&#195;&#182;lnir &#195;?sgeirsson)</author>
    </item>
  </channel>
</rss>
