DZone 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

Snippets has posted 5883 posts at DZone. View Full User Profile

Validating An Array Argument

02.24.2010
| 2114 views |
  • submit to reddit
        Source: <a href="http://github.com/sandal/rbp-book/raw/gh-pages/pdfs/ch02.pdf">Chapter 2 - Designing Beautiful APIs</a> [github.com] (O’Reilly’s Ruby Best Practices) via <a href="http://www.rubyinside.com/free-chapters-ruby-best-practices-3004.html">RubyInside.com</a>
<snip>
def distance4(*points)
  x1,y1,x2,y2 = points.flatten
  raise ArgumentError unless [x1,y1,x2,y2].all? { |e| Numeric === e }
  Math.hypot(x2 - x1, y2 - y1)
end
</snip>

Resources:
 - <a href="http://ruby-doc.org/core/classes/Numeric.html">Class: Numeric</a> [ruby-doc.org]