<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: add code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 16:01:38 GMT</pubDate>
    <description>DZone Snippets: add code</description>
    <item>
      <title>Increment a date using Ruby</title>
      <link>http://snippets.dzone.com/posts/show/5200</link>
      <description>This Ruby code converts a string into a date and increments the day, week, month, quarter or year.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def date_add(sdate='', unit='',i=0)&lt;br /&gt;&lt;br /&gt;  sdate[/(\d+)\/(\d+)\/(\d+)\s(\d+):(\d+):(\d+)/]&lt;br /&gt;  iyear = $3.to_i; imonth = $2.to_i; iday = $1.to_i; ihour = $4.to_i; imin = $5.to_i; isec = $6.to_i&lt;br /&gt;  &lt;br /&gt;  case  unit&lt;br /&gt;    when 'days'&lt;br /&gt;      t1 = Time.local(iyear,imonth,iday,ihour,imin,isec)&lt;br /&gt;      t1 += (60 * 60 * 24 * i)&lt;br /&gt;    when 'weeks'&lt;br /&gt;      t1 = Time.local(iyear,imonth,iday,ihour,imin,isec)&lt;br /&gt;      t1 += (60 * 60 * 24 * 7 * i) &lt;br /&gt;    when 'months'&lt;br /&gt;      imonth += i&lt;br /&gt;      if imonth &lt; 12 then&lt;br /&gt;        t1 = Time.local(iyear,imonth+i,iday,ihour,imin,isec)&lt;br /&gt;      else&lt;br /&gt;        t1 = Time.local(iyear+=1,imonth -12,iday,ihour,imin,isec)&lt;br /&gt;      end&lt;br /&gt;    when 'quarter'&lt;br /&gt;      imonth += 3&lt;br /&gt;      if imonth &lt;= 12 then&lt;br /&gt;        t1 = Time.local(iyear,imonth,iday,ihour,imin,isec)&lt;br /&gt;      else&lt;br /&gt;        t1 = Time.local(iyear+=1,imonth - 12,iday,ihour,imin,isec)&lt;br /&gt;      end    &lt;br /&gt;    when 'years'&lt;br /&gt;      t1 = Time.local(iyear+i,imonth,iday,ihour,imin,isec)&lt;br /&gt;    else&lt;br /&gt;      raise 'not a valid date unit'&lt;br /&gt;  end&lt;br /&gt;  t1&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;date_add("17/03/2008 17:48:00",'months',2)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;output: Sat May 17 17:48:00 +0100 2008&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 05 Mar 2008 13:43:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5200</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>svn-add-all-new-files</title>
      <link>http://snippets.dzone.com/posts/show/4722</link>
      <description>alias svn-add-all-new-files='svn st|grep ^?|sed s/?//|xargs svn add $1'</description>
      <pubDate>Wed, 31 Oct 2007 10:00:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4722</guid>
      <author>sessy (sessy)</author>
    </item>
    <item>
      <title>HTML/JavaScript - Select list - Add/Remove Options (DOM)</title>
      <link>http://snippets.dzone.com/posts/show/4442</link>
      <description>from http://www.mredkj.com/tutorials/tutorial005.html&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Overview&lt;br /&gt;&lt;br /&gt;    * Insert Before Selected - A new option is created and added above the selected option (as determined by selectedIndex). If none are selected, then no option is added.&lt;br /&gt;    * Remove Selected - Deletes the selected option (or options) from the list. If no options are selected, no options are deleted.&lt;br /&gt;    * Append Last - No matter what is selected, a new option is added at the end.&lt;br /&gt;    * Remove Last - No matter what is selected, the last option is deleted from the list.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Explanation&lt;br /&gt;&lt;br /&gt;According to DOM Level 1, the following is the syntax for the add and remove methods in HTMLSelectElement:&lt;br /&gt;&lt;br /&gt;void add(in HTMLElement element, in HTMLElement before) raises(DOMException);&lt;br /&gt;void remove(in long index);&lt;br /&gt;&lt;br /&gt;The add method takes two arguments: the element to add, and the element to insert before. The spec also says you can add to the end of the list by passing null as the second argument.&lt;br /&gt;&lt;br /&gt;The remove method just takes a number: the index of the option to be removed. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The JavaScript&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;script language="JavaScript" type="text/javascript"&gt;&lt;br /&gt;&lt;!--&lt;br /&gt;var count1 = 0;&lt;br /&gt;var count2 = 0;&lt;br /&gt;&lt;br /&gt;function insertOptionBefore(num)&lt;br /&gt;{&lt;br /&gt;  var elSel = document.getElementById('selectX');&lt;br /&gt;  if (elSel.selectedIndex &gt;= 0) {&lt;br /&gt;    var elOptNew = document.createElement('option');&lt;br /&gt;    elOptNew.text = 'Insert' + num;&lt;br /&gt;    elOptNew.value = 'insert' + num;&lt;br /&gt;    var elOptOld = elSel.options[elSel.selectedIndex];  &lt;br /&gt;    try {&lt;br /&gt;      elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE&lt;br /&gt;    }&lt;br /&gt;    catch(ex) {&lt;br /&gt;      elSel.add(elOptNew, elSel.selectedIndex); // IE only&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function removeOptionSelected()&lt;br /&gt;{&lt;br /&gt;  var elSel = document.getElementById('selectX');&lt;br /&gt;  var i;&lt;br /&gt;  for (i = elSel.length - 1; i&gt;=0; i--) {&lt;br /&gt;    if (elSel.options[i].selected) {&lt;br /&gt;      elSel.remove(i);&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function appendOptionLast(num)&lt;br /&gt;{&lt;br /&gt;  var elOptNew = document.createElement('option');&lt;br /&gt;  elOptNew.text = 'Append' + num;&lt;br /&gt;  elOptNew.value = 'append' + num;&lt;br /&gt;  var elSel = document.getElementById('selectX');&lt;br /&gt;&lt;br /&gt;  try {&lt;br /&gt;    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE&lt;br /&gt;  }&lt;br /&gt;  catch(ex) {&lt;br /&gt;    elSel.add(elOptNew); // IE only&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function removeOptionLast()&lt;br /&gt;{&lt;br /&gt;  var elSel = document.getElementById('selectX');&lt;br /&gt;  if (elSel.length &gt; 0)&lt;br /&gt;  {&lt;br /&gt;    elSel.remove(elSel.length - 1);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;//--&gt;&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The HTML&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;form&gt;&lt;br /&gt;&lt;input type="button" value="o" onclick="insertOptionBefore(count1++);" /&gt;&lt;br /&gt;Insert Before Selected&lt;br /&gt;&lt;br /&gt;&lt;input type="button" value="o" onclick="removeOptionSelected();" /&gt;&lt;br /&gt;Remove Selected&lt;br /&gt;&lt;br /&gt;&lt;select id="selectX" size="10" multiple="multiple"&gt;&lt;br /&gt;&lt;option value="original1" selected="selected"&gt;Orig1&lt;/option&gt;&lt;br /&gt;&lt;option value="original2"&gt;Orig2&lt;/option&gt;&lt;br /&gt;&lt;/select&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;input type="button" value="o" onclick="appendOptionLast(count2++);" /&gt;&lt;br /&gt;Append Last&lt;br /&gt;&lt;br /&gt;&lt;input type="button" value="o" onclick="removeOptionLast();" /&gt;&lt;br /&gt;Remove Last&lt;br /&gt;&lt;/form&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 22 Aug 2007 12:39:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4442</guid>
      <author>bradalyst (brad)</author>
    </item>
    <item>
      <title>Javascript Manipulate Class Names</title>
      <link>http://snippets.dzone.com/posts/show/2630</link>
      <description>I often have to manipulate class names of objects in javascript.&lt;br /&gt;But className can have multiple classes in it.&lt;br /&gt;These functions deal with that.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// ----------------------------------------------------------------------------&lt;br /&gt;// HasClassName&lt;br /&gt;//&lt;br /&gt;// Description : returns boolean indicating whether the object has the class name&lt;br /&gt;//    built with the understanding that there may be multiple classes&lt;br /&gt;//&lt;br /&gt;// Arguments:&lt;br /&gt;//    objElement              - element to manipulate&lt;br /&gt;//    strClass                - class name to add&lt;br /&gt;//&lt;br /&gt;function HasClassName(objElement, strClass)&lt;br /&gt;   {&lt;br /&gt;&lt;br /&gt;   // if there is a class&lt;br /&gt;   if ( objElement.className )&lt;br /&gt;      {&lt;br /&gt;&lt;br /&gt;      // the classes are just a space separated list, so first get the list&lt;br /&gt;      var arrList = objElement.className.split(' ');&lt;br /&gt;&lt;br /&gt;      // get uppercase class for comparison purposes&lt;br /&gt;      var strClassUpper = strClass.toUpperCase();&lt;br /&gt;&lt;br /&gt;      // find all instances and remove them&lt;br /&gt;      for ( var i = 0; i &lt; arrList.length; i++ )&lt;br /&gt;         {&lt;br /&gt;&lt;br /&gt;         // if class found&lt;br /&gt;         if ( arrList[i].toUpperCase() == strClassUpper )&lt;br /&gt;            {&lt;br /&gt;&lt;br /&gt;            // we found it&lt;br /&gt;            return true;&lt;br /&gt;&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;         }&lt;br /&gt;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;   // if we got here then the class name is not there&lt;br /&gt;   return false;&lt;br /&gt;&lt;br /&gt;   }&lt;br /&gt;// &lt;br /&gt;// HasClassName&lt;br /&gt;// ----------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// ----------------------------------------------------------------------------&lt;br /&gt;// AddClassName&lt;br /&gt;//&lt;br /&gt;// Description : adds a class to the class attribute of a DOM element&lt;br /&gt;//    built with the understanding that there may be multiple classes&lt;br /&gt;//&lt;br /&gt;// Arguments:&lt;br /&gt;//    objElement              - element to manipulate&lt;br /&gt;//    strClass                - class name to add&lt;br /&gt;//&lt;br /&gt;function AddClassName(objElement, strClass, blnMayAlreadyExist)&lt;br /&gt;   {&lt;br /&gt;&lt;br /&gt;   // if there is a class&lt;br /&gt;   if ( objElement.className )&lt;br /&gt;      {&lt;br /&gt;&lt;br /&gt;      // the classes are just a space separated list, so first get the list&lt;br /&gt;      var arrList = objElement.className.split(' ');&lt;br /&gt;&lt;br /&gt;      // if the new class name may already exist in list&lt;br /&gt;      if ( blnMayAlreadyExist )&lt;br /&gt;         {&lt;br /&gt;&lt;br /&gt;         // get uppercase class for comparison purposes&lt;br /&gt;         var strClassUpper = strClass.toUpperCase();&lt;br /&gt;&lt;br /&gt;         // find all instances and remove them&lt;br /&gt;         for ( var i = 0; i &lt; arrList.length; i++ )&lt;br /&gt;            {&lt;br /&gt;&lt;br /&gt;            // if class found&lt;br /&gt;            if ( arrList[i].toUpperCase() == strClassUpper )&lt;br /&gt;               {&lt;br /&gt;&lt;br /&gt;               // remove array item&lt;br /&gt;               arrList.splice(i, 1);&lt;br /&gt;&lt;br /&gt;               // decrement loop counter as we have adjusted the array's contents&lt;br /&gt;               i--;&lt;br /&gt;&lt;br /&gt;               }&lt;br /&gt;&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;         }&lt;br /&gt;&lt;br /&gt;      // add the new class to end of list&lt;br /&gt;      arrList[arrList.length] = strClass;&lt;br /&gt;&lt;br /&gt;      // add the new class to beginning of list&lt;br /&gt;      //arrList.splice(0, 0, strClass);&lt;br /&gt;      &lt;br /&gt;      // assign modified class name attribute&lt;br /&gt;      objElement.className = arrList.join(' ');&lt;br /&gt;&lt;br /&gt;      }&lt;br /&gt;   // if there was no class&lt;br /&gt;   else&lt;br /&gt;      {&lt;br /&gt;&lt;br /&gt;      // assign modified class name attribute      &lt;br /&gt;      objElement.className = strClass;&lt;br /&gt;   &lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;   }&lt;br /&gt;// &lt;br /&gt;// AddClassName&lt;br /&gt;// ----------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// ----------------------------------------------------------------------------&lt;br /&gt;// RemoveClassName&lt;br /&gt;//&lt;br /&gt;// Description : removes a class from the class attribute of a DOM element&lt;br /&gt;//    built with the understanding that there may be multiple classes&lt;br /&gt;//&lt;br /&gt;// Arguments:&lt;br /&gt;//    objElement              - element to manipulate&lt;br /&gt;//    strClass                - class name to remove&lt;br /&gt;//&lt;br /&gt;function RemoveClassName(objElement, strClass)&lt;br /&gt;   {&lt;br /&gt;&lt;br /&gt;   // if there is a class&lt;br /&gt;   if ( objElement.className )&lt;br /&gt;      {&lt;br /&gt;&lt;br /&gt;      // the classes are just a space separated list, so first get the list&lt;br /&gt;      var arrList = objElement.className.split(' ');&lt;br /&gt;&lt;br /&gt;      // get uppercase class for comparison purposes&lt;br /&gt;      var strClassUpper = strClass.toUpperCase();&lt;br /&gt;&lt;br /&gt;      // find all instances and remove them&lt;br /&gt;      for ( var i = 0; i &lt; arrList.length; i++ )&lt;br /&gt;         {&lt;br /&gt;&lt;br /&gt;         // if class found&lt;br /&gt;         if ( arrList[i].toUpperCase() == strClassUpper )&lt;br /&gt;            {&lt;br /&gt;&lt;br /&gt;            // remove array item&lt;br /&gt;            arrList.splice(i, 1);&lt;br /&gt;&lt;br /&gt;            // decrement loop counter as we have adjusted the array's contents&lt;br /&gt;            i--;&lt;br /&gt;&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;         }&lt;br /&gt;&lt;br /&gt;      // assign modified class name attribute&lt;br /&gt;      objElement.className = arrList.join(' ');&lt;br /&gt;&lt;br /&gt;      }&lt;br /&gt;   // if there was no class&lt;br /&gt;   // there is nothing to remove&lt;br /&gt;&lt;br /&gt;   }&lt;br /&gt;// &lt;br /&gt;// RemoveClassName&lt;br /&gt;// ----------------------------------------------------------------------------&lt;br /&gt;  &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 20 Sep 2006 21:34:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2630</guid>
      <author>Will_Rickards (Will Rickards)</author>
    </item>
    <item>
      <title>Addition for hashes in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/1535</link>
      <description>Why, oh, why is there no addition for hashes in Ruby? 'update' can do the trick, but only returns what was updated, rather than the whole updated hash. It also forces an update rather than being passive.&lt;br /&gt;&lt;br /&gt;Sometimes you only want to temporarily do an add, and this is how I pulled it off:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;class Hash&lt;br /&gt;  def +(add)&lt;br /&gt;    temp = {}&lt;br /&gt;    add.each{|k,v| temp[k] = v}&lt;br /&gt;    self.each{|k,v| temp[k] = v}&lt;br /&gt;    temp&lt;br /&gt;  end&lt;br /&gt;end&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Now you can do stuff like:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;x = { :a =&gt; 1 }&lt;br /&gt;y = { :b =&gt; 2 }&lt;br /&gt;&lt;br /&gt;# x + y =&gt; { :a =&gt; 1, :b =&gt; 2 }, but x and y are untouched&lt;br /&gt;# x + { :c =&gt; 3 } =&gt; { :a =&gt; 1, :c =&gt; 3 }&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;If you want to force an add, it's easy:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;x += y =&gt; { :a =&gt; 1, :b =&gt; 2 } (this is now what x contains)&lt;/code&gt;</description>
      <pubDate>Mon, 20 Feb 2006 08:48:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1535</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
    <item>
      <title>add face to pane snippet</title>
      <link>http://snippets.dzone.com/posts/show/1198</link>
      <description>&lt;code&gt;&lt;br /&gt;add-button: does [&lt;br /&gt;    append lay/pane layout/offset [&lt;br /&gt;        button [alert "I'm new!"]&lt;br /&gt;    ] 100x100&lt;br /&gt;    show lay&lt;br /&gt;]&lt;br /&gt;view lay: layout [size 400x300 button [add-button]]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 19 Jan 2006 03:15:07 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1198</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>add face to pane snippet</title>
      <link>http://snippets.dzone.com/posts/show/1197</link>
      <description>&lt;code&gt;&lt;br /&gt;add-button: does [&lt;br /&gt;    append lay/pane layout/offset [&lt;br /&gt;        button [alert "I'm new!"]&lt;br /&gt;    ] 100x100&lt;br /&gt;    show lay&lt;br /&gt;]&lt;br /&gt;view lay: layout [size 400x300 button [add-button]]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 19 Jan 2006 03:14:14 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1197</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
  </channel>
</rss>
