<?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>Sat, 26 Jul 2008 17:26:21 GMT</pubDate>
    <description>DZone Snippets: ruby 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>Alphabetical sorter</title>
      <link>http://snippets.dzone.com/posts/show/2705</link>
      <description>// This function sorts an array of objects that include this code, alphabetically&lt;br /&gt;// That's nothing new, except this sorts strings starting with a number correctly!&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  # ---&lt;br /&gt;  # Sorting&lt;br /&gt;  # ---&lt;br /&gt;def &lt;=&gt;(other)&lt;br /&gt;    regex = /^[\d]+/&lt;br /&gt;    if self.title =~ regex&lt;br /&gt;      our_num = Regexp.last_match[0].to_i&lt;br /&gt;      if other.title =~ regex&lt;br /&gt;        other_num = Regexp.last_match[0].to_i&lt;br /&gt;        return our_num &lt;=&gt; other_num&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;    self.title &lt;=&gt; other.title&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 23 Sep 2006 19:15:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2705</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>
    <item>
      <title>Youtube url code</title>
      <link>http://snippets.dzone.com/posts/show/2522</link>
      <description>This is a part of a ruby-cocoa app I'm writing.&lt;br /&gt;(TubeSock is just terrible)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'net/http'&lt;br /&gt;require 'uri'&lt;br /&gt;&lt;br /&gt;class YouTubeMovie&lt;br /&gt;  attr_accessor :viewer_url, :movie_url, :get_params&lt;br /&gt;  def initialize(url)&lt;br /&gt;    @viewer_url = url&lt;br /&gt;    setMovieURL&lt;br /&gt;  end&lt;br /&gt;  def setMovieURL&lt;br /&gt;    # Get the viewer page html&lt;br /&gt;    req = Net::HTTP::Get.new @viewer_url&lt;br /&gt;    viewer_page = nil&lt;br /&gt;    res = Net::HTTP.start(@viewer_url.host, @viewer_url.port) do |request|&lt;br /&gt;      viewer_page = request.request(req)&lt;br /&gt;    end&lt;br /&gt;    # Extract the required info.&lt;br /&gt;    # in the html there's a line like so:&lt;br /&gt;    # var fo = new SWFObject("/player2.swf?video_id=AUnPDmmnF0U&amp;l=1363&amp;t=OEgsToPDskJUmKC_b_nXO_yUrNLKSY18&amp;nc=13369344", "movie_player", "450", "370", 7, "#FFFFFF");&lt;br /&gt;    # we want to extract the ?video_id=AUnPDmmnF0U&amp;l=1363&amp;t=OEgsToPDskJUmKC_b_nXO_yUrNLKSY18&amp;nc=13369344&lt;br /&gt;    # this regex does that...&lt;br /&gt;    regex = Regexp.new(/\?video_id=[\w]+&amp;l=[\w]+&amp;t=[\w]+&amp;nc=[\d]+/)&lt;br /&gt;    @get_params = regex.match(viewer_page.body).to_s&lt;br /&gt;    @movie_url = URI.parse('http://www.youtube.com/get_video' + @get_params)&lt;br /&gt;    p 'movie url: ' + @movie_url.to_s&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 03 Sep 2006 16:05:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2522</guid>
      <author>aptiva (Fj&#195;&#182;lnir &#195;?sgeirsson)</author>
    </item>
  </channel>
</rss>
