Wednesday, 13 August 2014

extract_options

def options(*args)
args.extract_options!
end
options(1, 2) # => {}
options(1, 2, :a => :b) # => {:a=>:b}
# Use args with the splat operation to allow
# an unlimited number of parameters
def my_method(*args)
options = args.extract_options!
puts "Arguments: #{args.inspect}"
puts "Options: #{options.inspect}"
end
my_method(1, 2)
# Arguments: [1, 2]
# Options: {}
my_method(1, 2, :a => :b)
# Arguments: [1, 2]
# Options: {:a=>:b}
#http://simonecarletti.com/blog/2009/09/inside-ruby-on-rails-extract_options-from-arrays/
view raw gistfile1.rb hosted with ❤ by GitHub

No comments:

Post a Comment