<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
  <channel>
    <title>CodeKeep Ruby Feed</title>
    <description>The latest and greatest Ruby code snippets publicly available</description>
    <link>http://www.codekeep.net/feeds.aspx</link>
    <lastBuildDate>Fri, 02 Jan 2009 06:50:06 GMT</lastBuildDate>
    <docs>http://backend.userland.com/rss</docs>
    <generator>RSS.NET: http://www.rssdotnet.com/</generator>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/CodeKeepRuby" /><feedburner:info uri="codekeepruby" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
      <title>Numerical Arrays in Ruby</title>
      <description>Description: This code demonstrates the addition of numerical array capability to Ruby similar to what can be achieved via numpy.&lt;br /&gt;&lt;br /&gt;Link: &lt;a href='http://www.codekeep.net/snippets/6cae6dc5-031b-4efd-afe4-63b5266303d9.aspx'&gt;http://www.codekeep.net/snippets/6cae6dc5-031b-4efd-afe4-63b5266303d9.aspx&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style='font-size: 9pt;'&gt;#!/usr/bin/env ruby -w -KU
# numarray.rb
$KCODE=&amp;quot;UTF-8&amp;quot;
=begin
This module demonstrates a numerical array similar to Numpy in
Python or some of the things you can do in MATLAB.

Author: Michael O'Keefe
Date: December 1, 2008
http://www.okeefecreations.com
=end


module NumArray
    def map_elements(&amp;amp;block)
        tail.zip(init).map(&amp;amp;block)
    end
  
    def diff
        map_elements { | x | x[0] - x[1] }
    end

    def average
        map_elements { | x | 0.5 * (x[0] + x[1]) }
    end

    def plus(other)
        zip(other).map { | x | x[0] + x[1] }
    end

    def minus(other)
        zip(other).map { | x | x[0] - x[1] }
    end

    def times(other)
        zip(other).map { | x | x[0] * x[1] }
    end

    def divide(other)
        zip(other).map do | x |
            if x[1] != 0.0 then
                x[0] / x[1]
            else
                0.0
            end
        end
    end

    def pow(n)
        map { | x | x ** n }
    end

    def increasing?
        tail.zip(init).all? { | x | x[0] &amp;gt; x[1] }
    end
    
    def decreasing?
        tail.zip(init).all? { | x | x[0] &amp;lt; x[1] }
    end

    def cubed_avg
        map_elements do | x |
            x0 = x[0]
            x1 = x[1]
            (x0 ** 3 + x0 ** 2 * x1 + x0 * x1 ** 2 + x1 ** 3) / 4.0
        end
    end

    def squared_avg
        map_elements do | x |
            x0 = x[0]
            x1 = x[1]
            (x0 ** 2 + x0 * x1 + x1 ** 2) / 3.0
        end
    end

    def tail
        self[1..-1]
    end

    def init
        self[0..-2]
    end

    def head
        self[0]
    end

    def last
        self[-1]
    end
end


class Array
  include NumArray
end

#----------------------------------------------
#!/usr/bin/env ruby -w -KU
# testnumarray.rb
$KCODE=&amp;quot;UTF-8&amp;quot;
=begin
This is a unit test module for numarray.rb

Author: Michael O'Keefe
Date: December 1, 2008
http://www.okeefecreations.com
=end
require 'test/unit'
require 'numarray'


class TestNumArray &amp;lt; Test::Unit::TestCase
    def setup
        @a1 = [1.0, 2.0, 3.0, 4.0, 5.0]
        @a2 = [10.0, 20.0, 30.0, 40.0, 50.0]
        @a3 = [2.0, 2.0, 2.0, 2.0]
    end
    
    def test_diff
        assert_equal(@a1.diff,
            [1.0, 1.0, 1.0, 1.0]
        )
    end
    
    def test_average
        assert_equal(@a1.average,
            [1.5, 2.5, 3.5, 4.5]
        )
    end
    
    def test_plus
        assert_equal(@a1.plus(@a2),
            [11.0, 22.0, 33.0, 44.0, 55.0]
        )
    end
    
    def test_minus
        assert_equal(@a1.minus(@a2),
            [-9.0, -18.0, -27.0, -36.0, -45.0]
        )
    end
    
    def test_times
        assert_equal(@a1.times(@a2),
            [10.0, 40.0, 90.0, 160.0, 250.0]
        )
    end
    
    def test_divide
        assert_equal(@a1.divide(@a2),
            [0.1, 0.1, 0.1, 0.1, 0.1]
        )
    end
    
    def test_pow
        assert_equal(@a1.pow(2),
            [1.0, 4.0, 9.0, 16.0, 25.0]
        )
    end
    
    def test_increasing?
        assert_equal(@a1.increasing?, true)
    end
    
    def test_decreasing?
        assert_equal(@a1.decreasing?, false)
    end

    def test_cubed_avg
        assert_equal(@a3.cubed_avg, [8.0, 8.0, 8.0])
    end

    def test_squared_avg
        assert_equal(@a3.squared_avg, [4.0, 4.0, 4.0])
    end
    
    def test_tail
        assert_equal(@a1.tail, [2.0, 3.0, 4.0, 5.0])
    end
    
    def test_init
        assert_equal(@a1.init, [1.0, 2.0, 3.0, 4.0])
    end
    
    def test_head
        assert_equal(@a1.head, 1.0)
    end
    
    def test_last
        assert_equal(@a1.last, 5.0)
    end
end&lt;/pre&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeKeepRuby/~4/j4yd0TlOb4I" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/CodeKeepRuby/~3/j4yd0TlOb4I/6cae6dc5-031b-4efd-afe4-63b5266303d9.aspx</link>
      <pubDate>Fri, 02 Jan 2009 06:50:06 GMT</pubDate>
    <feedburner:origLink>http://www.codekeep.net/snippets/6cae6dc5-031b-4efd-afe4-63b5266303d9.aspx</feedburner:origLink></item>
    <item>
      <title>Módulo Ruby con constantes útiles.</title>
      <description>Description: Es un módulo en Ruby que define constantes globales de una aplicación. En el momento solamente define la expresión regular utilizada para validar un email.&lt;br /&gt;&lt;br /&gt;Link: &lt;a href='http://www.codekeep.net/snippets/3b2495b9-d38e-4283-8cd8-2632e2163793.aspx'&gt;http://www.codekeep.net/snippets/3b2495b9-d38e-4283-8cd8-2632e2163793.aspx&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style='font-size: 9pt;'&gt;module Utilidades::Constantes
  # Establecer la expresi&amp;#243;n regular para validar los email
  EXPREG_EMAIL = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
end&lt;/pre&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeKeepRuby/~4/KAXgUC5H66k" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/CodeKeepRuby/~3/KAXgUC5H66k/3b2495b9-d38e-4283-8cd8-2632e2163793.aspx</link>
      <pubDate>Fri, 25 May 2007 16:16:29 GMT</pubDate>
    <feedburner:origLink>http://www.codekeep.net/snippets/3b2495b9-d38e-4283-8cd8-2632e2163793.aspx</feedburner:origLink></item>
    <item>
      <title>Clase que genera aleatorios</title>
      <description>Description: Esta es una clase en Ruby que genera aleatorios&lt;br /&gt;&lt;br /&gt;Link: &lt;a href='http://www.codekeep.net/snippets/1f074d22-e770-4d15-b40b-bf61bd994569.aspx'&gt;http://www.codekeep.net/snippets/1f074d22-e770-4d15-b40b-bf61bd994569.aspx&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style='font-size: 9pt;'&gt;class Utilidades::GeneradorAleatorio

  # genera una palabra aleatoria con la cantidad de caracteres especificada
  # utiliza las letras de la 'a' a la 'z' min&amp;#250;sculas y masy&amp;#250;sculas y los n&amp;#250;meros del '0' al '9'
  def self.generar_palabra(cantidad = 10)
    return self.generar(cantidad) { (&amp;quot;a&amp;quot;..&amp;quot;z&amp;quot;).to_a + (&amp;quot;A&amp;quot;..&amp;quot;Z&amp;quot;).to_a + (&amp;quot;0&amp;quot;..&amp;quot;9&amp;quot;).to_a }
  end
  
  # genera una n&amp;#250;mero aleatorio con la cantidad de d&amp;#237;gitos especificada
  # utiliza los n&amp;#250;meros del '0' al '9'
  def self.generar_numero(cantidad = 10)
    return self.generar(cantidad) { (&amp;quot;0&amp;quot;..&amp;quot;9&amp;quot;).to_a }
  end
  
  # genera una cadena de caracteres aleatoria
  # debe proveerse un bloque que retorne un arreglo de donde se sacar&amp;#225;n los caracteres
  def self.generar(cantidad = 10)
    chars = yield
    random_key = &amp;quot;&amp;quot;
    1.upto(cantidad) { |i| random_key &amp;lt;&amp;lt; chars[rand(chars.size)] }
    return random_key
  end
end&lt;/pre&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeKeepRuby/~4/obfXgcPa9_I" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/CodeKeepRuby/~3/obfXgcPa9_I/1f074d22-e770-4d15-b40b-bf61bd994569.aspx</link>
      <pubDate>Wed, 07 Mar 2007 14:52:12 GMT</pubDate>
    <feedburner:origLink>http://www.codekeep.net/snippets/1f074d22-e770-4d15-b40b-bf61bd994569.aspx</feedburner:origLink></item>
    <item>
      <title>Multi-select list boxes in Rails</title>
      <description>Description: Muestra como crear una lista selección múltiple y como obtener los valores luego en el controlador.&lt;br /&gt;&lt;br /&gt;Link: &lt;a href='http://www.codekeep.net/snippets/73bffc67-991b-4231-a2ee-fdce2fdb5a26.aspx'&gt;http://www.codekeep.net/snippets/73bffc67-991b-4231-a2ee-fdce2fdb5a26.aspx&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style='font-size: 9pt;'&gt;# para contruir la lista en la vista
&amp;lt;%= select_tag 'flavors[]',  
  options_for_select( 
    [['vanilla','1'], ['strawberry','2'], ['chocolate','3']] ),
  { :multiple =&amp;gt; true, :size =&amp;gt;5 } 
%&amp;gt;

# para obtener los valores en el controlador
@sunday = IceCreamSunday.create(params[:sunday])    
unless has_errors?(@sunday)
  @sunday.flavors &amp;lt;&amp;lt; Flavor.find(params[:flavors].collect{|char| char.to_i})
  redirect_to make_the_dish_url(:id =&amp;gt; @sunday.id)
end  &lt;/pre&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeKeepRuby/~4/Ftrm-lWU5Ho" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/CodeKeepRuby/~3/Ftrm-lWU5Ho/73bffc67-991b-4231-a2ee-fdce2fdb5a26.aspx</link>
      <pubDate>Wed, 14 Feb 2007 16:48:04 GMT</pubDate>
    <feedburner:origLink>http://www.codekeep.net/snippets/73bffc67-991b-4231-a2ee-fdce2fdb5a26.aspx</feedburner:origLink></item>
    <item>
      <title>Create a random password</title>
      <description>Description: Creates a random password using numbers and letters&lt;br /&gt;&lt;br /&gt;Link: &lt;a href='http://www.codekeep.net/snippets/e41fb216-7061-41b0-8612-2c258d51ef68.aspx'&gt;http://www.codekeep.net/snippets/e41fb216-7061-41b0-8612-2c258d51ef68.aspx&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style='font-size: 9pt;'&gt;private
def newpass( len )
  chars = (&amp;quot;a&amp;quot;..&amp;quot;z&amp;quot;).to_a + (&amp;quot;1&amp;quot;..&amp;quot;9&amp;quot;).to_a
  newpass = &amp;quot;&amp;quot;
  1.upto(len) { |i| newpass &amp;lt;&amp;lt; chars[rand(chars.size-1)] }
  return newpass
end&lt;/pre&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeKeepRuby/~4/F87IOG3wVQ8" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/CodeKeepRuby/~3/F87IOG3wVQ8/e41fb216-7061-41b0-8612-2c258d51ef68.aspx</link>
      <pubDate>Tue, 14 Feb 2006 12:29:16 GMT</pubDate>
    <feedburner:origLink>http://www.codekeep.net/snippets/e41fb216-7061-41b0-8612-2c258d51ef68.aspx</feedburner:origLink></item>
    <item>
      <title>Hash Password</title>
      <description>Description: Hashes a password using SHA1&lt;br /&gt;&lt;br /&gt;Link: &lt;a href='http://www.codekeep.net/snippets/5d3afb89-c35d-40c4-b906-520f2ed817c6.aspx'&gt;http://www.codekeep.net/snippets/5d3afb89-c35d-40c4-b906-520f2ed817c6.aspx&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style='font-size: 9pt;'&gt;def self.hash_password(pass)
  Digest::SHA1.hexdigest(pass)
end&lt;/pre&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeKeepRuby/~4/7Qll1A0V3eQ" height="1" width="1"/&gt;</description>
      <link>http://feedproxy.google.com/~r/CodeKeepRuby/~3/7Qll1A0V3eQ/5d3afb89-c35d-40c4-b906-520f2ed817c6.aspx</link>
      <pubDate>Tue, 14 Feb 2006 12:26:41 GMT</pubDate>
    <feedburner:origLink>http://www.codekeep.net/snippets/5d3afb89-c35d-40c4-b906-520f2ed817c6.aspx</feedburner:origLink></item>
  </channel>
</rss>

