Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Danger http://6brand.com

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Convert an array to a hash

Pretty simple, but maybe not intuitive
   1  
   2  class Array
   3    def to_h
   4      Hash[*self]
   5    end
   6  end

Use it like this:
   1  
   2  ['action', 'go', 'id', 6].to_h   # => { 'action' => 'go', 'id' => 6 }

Count Values in an Array

This modifies the Array class to allow for easy summing
   1  
   2  class Array
   3    def total
   4      t = 0
   5      self.each { |v| t += v }
   6      t
   7    end
   8  end

Use it like this:
   1  
   2  [1,2,2,3].total  # => 8

link_to_remote_unless_current

// Full discussion about this available at:
// http://6brand.com/articles/2006/06/07/link_to_remote_unless_current

   1  
   2  # throw this in one of your controller helpers.
   3  # it works just like a combination of link_to_unless_current and link_to_remote
   4  def link_to_remote_unless_current(name, options = {}, html_options = {}, *parameters_for_method_reference, &block)
   5    if current_page?(options[:url])
   6      if block_given?
   7        block.arity <= 1 ? yield(name) : yield(name, remote_function(options), html_options, *parameters_for_method_reference)
   8      else
   9        name
  10      end
  11    else
  12      link_to_function(name, remote_function(options), html_options)
  13    end
  14  end
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS