<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: range code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 18 May 2008 06:16:21 GMT</pubDate>
    <description>DZone Snippets: range code</description>
    <item>
      <title>Ping a range of IP addresses</title>
      <link>http://snippets.dzone.com/posts/show/5210</link>
      <description>&lt;code&gt;&lt;br /&gt;(1..254).each {|i| puts ": found 192.168.1.#{i}" if  `ping 192.168.1.#{i} -c 1 -w 1`.match(/ttl/) }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 11 Mar 2008 09:06:19 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5210</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Convert Ruby Array to Ranges</title>
      <link>http://snippets.dzone.com/posts/show/4677</link>
      <description># Array#to_ranges&lt;br /&gt;# Converts an array of values (which must respond to #succ) to an array of ranges. For example,&lt;br /&gt;# [3,4,5,1,6,9,8].to_ranges =&gt; [1,3..6,8..9] &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Array&lt;br /&gt;  def to_ranges&lt;br /&gt;    array = self.compact.uniq.sort&lt;br /&gt;    ranges = []&lt;br /&gt;    if !array.empty?&lt;br /&gt;      # Initialize the left and right endpoints of the range&lt;br /&gt;      left, right = self.first, nil&lt;br /&gt;      array.each do |obj|&lt;br /&gt;        # If the right endpoint is set and obj is not equal to right's successor &lt;br /&gt;        # then we need to create a range.&lt;br /&gt;        if right &amp;&amp; obj != right.succ&lt;br /&gt;          ranges &lt;&lt; Range.new(left,right)&lt;br /&gt;          left = obj&lt;br /&gt;        end&lt;br /&gt;        right = obj&lt;br /&gt;      end&lt;br /&gt;      ranges &lt;&lt; Range.new(left,right)&lt;br /&gt;    end&lt;br /&gt;    ranges&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 19 Oct 2007 13:03:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4677</guid>
      <author>bsiggelkow (Bill Siggelkow)</author>
    </item>
    <item>
      <title>Collapse ranges in arrays</title>
      <link>http://snippets.dzone.com/posts/show/4099</link>
      <description>Takes an Array and turns consecutive integers into Range objects.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Array&lt;br /&gt;    def collapse_ranges&lt;br /&gt;        return self if self.length &lt;= 2&lt;br /&gt;        self.uniq!&lt;br /&gt;        self.sort! rescue nil&lt;br /&gt;        temp_array, return_array = [], []&lt;br /&gt;        self.each_with_index do |item, i|&lt;br /&gt;            if item.respond_to?(:next)&lt;br /&gt;                temp_array.push item&lt;br /&gt;                if item.next != self[i + 1]&lt;br /&gt;                    return_array.concat 3 &lt;= temp_array.length ?&lt;br /&gt;                        [temp_array.first..temp_array.last] :&lt;br /&gt;                         temp_array&lt;br /&gt;                    temp_array.clear&lt;br /&gt;                end&lt;br /&gt;            else&lt;br /&gt;                return_array.concat 3 &lt;= temp_array.length ?&lt;br /&gt;                    [temp_array.first..temp_array.last] :&lt;br /&gt;                     temp_array&lt;br /&gt;                temp_array.clear&lt;br /&gt;                return_array.push item&lt;br /&gt;            end&lt;br /&gt;        end&lt;br /&gt;        return return_array&lt;br /&gt;    end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt; [1, 3, 4, 5, 7, 8].collapse_ranges.join(', ')&lt;br /&gt;=&gt; 1, 3..5, 7, 8&lt;br /&gt;&gt;&gt; %w(a c d e g i j).collapse_ranges.join(', ')&lt;br /&gt;=&gt; a, c..e, g, i, j&lt;br /&gt;&gt;&gt; [1, 2.5, 3, 4, 5].collapse_ranges.join(', ')&lt;br /&gt;=&gt; 1, 2.5, 3..5&lt;br /&gt;&gt;&gt; [1, 2, 3, 4, {:test =&gt; 'value'}, 5].collapse_ranges.join(', ')&lt;br /&gt;=&gt; 1..4, testvalue, 5&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 03 Jun 2007 17:59:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4099</guid>
      <author>sporkyy (Todd Sayre)</author>
    </item>
    <item>
      <title>Ruby: make ranges always include the last value</title>
      <link>http://snippets.dzone.com/posts/show/3808</link>
      <description>So, in conjunction with other snipped about setting up intervals on Time, this can be added to Range to make sure it always includes the last value, even if the interval doesn't directly land you on the end value&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Range&lt;br /&gt;  def each(options = {}, &amp;block)&lt;br /&gt;    val = self.begin&lt;br /&gt;    while val &lt; self.end&lt;br /&gt;      yield val&lt;br /&gt;      val = val.succ&lt;br /&gt;    end&lt;br /&gt;    yield self.end if self.end == val || options[:always_include_last]&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 12 Apr 2007 17:36:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3808</guid>
      <author>technodolt (Luke Ivers)</author>
    </item>
    <item>
      <title>Find missing array items</title>
      <link>http://snippets.dzone.com/posts/show/3322</link>
      <description>This works fine for integers and letters.  It should work for anything though, so long as Ruby can form an range from the first and last array items.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Array&lt;br /&gt;    def missing_items&lt;br /&gt;        return [] if self.length &lt;= 1&lt;br /&gt;        self.uniq!&lt;br /&gt;        self.sort! rescue nil&lt;br /&gt;        begin&lt;br /&gt;            (self.first..self.last).to_a - self&lt;br /&gt;        rescue&lt;br /&gt;            []&lt;br /&gt;        end&lt;br /&gt;    end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt; [1, 3, 4, 10].missing_items.join(', ')&lt;br /&gt;=&gt; 2, 5, 6, 7, 8, 9&lt;br /&gt;&gt;&gt; [1, 2, 7, 7.5, 8.2].missing_items.join(', ')&lt;br /&gt;=&gt; 3, 4, 5, 6, 8&lt;br /&gt;&gt;&gt; %w(a b c f g j).missing_items.join(', ')&lt;br /&gt;=&gt; d, e, h, i&lt;br /&gt;&gt;&gt; [2.5, {:test =&gt; 'value'}].missing_items.join(', ')&lt;br /&gt;=&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 18 Jan 2007 08:15:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3322</guid>
      <author>sporkyy (Todd Sayre)</author>
    </item>
    <item>
      <title>A range function with float increment</title>
      <link>http://snippets.dzone.com/posts/show/1526</link>
      <description>Taken from Edvard Majakari's comment in this &lt;a href=http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66472&gt;recipe&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def arange(start, stop=None, step=None):&lt;br /&gt;    if stop is None:&lt;br /&gt;        stop = float(start)&lt;br /&gt;        start = 0.0&lt;br /&gt;    if step is None:&lt;br /&gt;        step = 1.0&lt;br /&gt;    cur = float(start)&lt;br /&gt;    while cur &lt; stop:&lt;br /&gt;        yield cur&lt;br /&gt;        cur += step&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;For python 2.2 (e.g. pys60) you need to do a "from __future__ import generators" first.&lt;br /&gt;To get the list from the generator, use list(arange(...))</description>
      <pubDate>Sat, 18 Feb 2006 20:45:03 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1526</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
