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

Extract Options From Args

12.17.2009
| 6268 views |
  • submit to reddit
        If we need a method with  an arbitrary number of parameters (:arg1, :arg2…) followed by a list of keyword/value options we can use the ActiveSupport method #extract_options! to find the optinons. 
def my_method(*args)
  options = args.extract_options!
  puts "Arguments:  #{args.inspect}"
  puts "Options:    #{options.inspect}"
end
See: http://www.simonecarletti.com/blog/2009/09/inside-ruby-on-rails-extract_options-from-arrays/