<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Vinterbleg's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 04:28:13 GMT</pubDate>
    <description>DZone Snippets: Vinterbleg's Code Snippets</description>
    <item>
      <title>With object do...</title>
      <link>http://snippets.dzone.com/posts/show/5321</link>
      <description>This code enters the scope of an object, so you can temporarily avoid having to reference it by name.&lt;br /&gt; &lt;br /&gt;Effectively changes the value of 'self' in the block scope. This also enables you to run private methods on the object without using __send__.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def with(object, &amp;block)&lt;br /&gt;    object.instance_eval(&amp;block)&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example of usage:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;numbers = [1, 2, 3]&lt;br /&gt;&lt;br /&gt;with numbers do&lt;br /&gt;	map! { |n| n + 100 }&lt;br /&gt;	reject! { |n| n % 2 == 0}&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;p numbers # =&gt; [101, 103]&lt;br /&gt;&lt;br /&gt;n = 15&lt;br /&gt;n = with n do&lt;br /&gt;    self + 13&lt;br /&gt;end&lt;br /&gt;p n # =&gt; 28&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This gets much more interesting with complex objects that have lots of attributes and you want strict control over how they're set.</description>
      <pubDate>Thu, 03 Apr 2008 11:03:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5321</guid>
      <author>vinterbleg (Simon Ask Ulsnes)</author>
    </item>
    <item>
      <title>In-place map_with_index</title>
      <link>http://snippets.dzone.com/posts/show/5119</link>
      <description>// This code is similar to Enumerable#map, but like each_with_index, it also yields the index of the current element.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Array&lt;br /&gt;    def map_with_index!&lt;br /&gt;       each_with_index do |e, idx| self[idx] = yield(e, idx); end&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    def map_with_index(&amp;block)&lt;br /&gt;        dup.map_with_index!(&amp;block)&lt;br /&gt;    end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 08 Feb 2008 10:29:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5119</guid>
      <author>vinterbleg (Simon Ask Ulsnes)</author>
    </item>
    <item>
      <title>Map array to hash, with custom key and value</title>
      <link>http://snippets.dzone.com/posts/show/4805</link>
      <description>This code creates a hash from an array, using a block to determine the keys and values of the hash.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Array&lt;br /&gt;	def map_to_hash&lt;br /&gt;		map { |e| yield e }.inject({}) { |carry, e| carry.merge! e }&lt;br /&gt;	end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Use like this:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;[1, 2, 3].map_to_hash { |e| {e =&gt; e + 100} } # =&gt; {1 =&gt; 101, 2 =&gt; 102, 3 =&gt; 103}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 21 Nov 2007 10:29:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4805</guid>
      <author>vinterbleg (Simon Ask Ulsnes)</author>
    </item>
  </channel>
</rss>
