This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
No comments:
Post a Comment