<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Our Geek Space &#187; array</title>
	<atom:link href="http://blog.moove-it.com/tag/array/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.moove-it.com</link>
	<description>be free to express yourself...</description>
	<lastBuildDate>Thu, 26 Jan 2012 19:30:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Improving the ruby Array class without degrade performance</title>
		<link>http://blog.moove-it.com/improving-the-ruby-array-class-without-degrade-performance/</link>
		<comments>http://blog.moove-it.com/improving-the-ruby-array-class-without-degrade-performance/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 18:29:14 +0000</pubDate>
		<dc:creator>Pablo Ifran</dc:creator>
				<category><![CDATA[moove-it]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.moove-it.com/?p=131</guid>
		<description><![CDATA[Improving the ruby Array class Many times, when you are iterating over an array you want to know the last or the first element to do something. Unfortunately ruby don&#8217;t provide any method to help you doing this task. So, if you want a trick to do this, you can add this few lines in [...]]]></description>
			<content:encoded><![CDATA[<p>Improving the ruby Array class</p>
<p>Many times, when you are iterating over an array you want to know the last or the first element to do something. Unfortunately ruby don&#8217;t provide any method to help you doing this task.<br />
So, if you want a trick to do this, you can add this few lines in a rails project inside an initalizer.</p>
<p>Put this code in initializers/array.rb</p>
<pre>class Array

  #     array.each_with_first {|item, first| block }   -&gt;   array
  #
  #
  # Call a <em>block</em> ones per element, passing
  # the element and the first element of the array as parameters.
  #
  #    a = [ "a", "b", "c" ]
  #    a.each_with_first {|x, f| print "#{x} -- #{f} | " }
  #
  # produces:
  #
  #    a -- a | b -- a | c -- a |
  #
  def each_with_first(&amp;block)
    first = self.first

    self.each do |element|
      yield element, first
    end
  end

  #     array.each_with_last {|item, last| block }   -&gt;   array
  #
  #
  # Call a <em>block</em> ones per element, passing
  # the element and the last element of the array as parameters.
  #
  #    a = [ "a", "b", "c" ]
  #    a.each_with_last {|x, f| print "#{x} -- #{f} | " }
  #
  # produces:
  #
  #    a -- c | b -- c | c -- c |
  #
  def each_with_last(&amp;block)
    last = self.last

    self.each do |element|
      yield element, last
    end
  end

  #     array.each_with_first_and_last {|item, first, last| block }   -&gt;   array
  #
  #
  # Call a <em>block</em> ones per element, passing
  # the element, the first and the last element of the array as parameters.
  #
  #    a = [ "a", "b", "c" ]
  #    a.each_with_last {|x, f, l| print "#{x} -- #{f} -- #{l} | " }
  #
  # produces:
  #
  #    a -- a -- c | b -- a -- c | c -- a -- c |
  #
  def each_with_first_and_last(&amp;block)
    first = self.first
    last = self.last

    self.each do |element|
      yield element, first, last
    end
  end

end</pre>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://blog.moove-it.com/improving-the-ruby-array-class-without-degrade-performance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

