saasbook

optional_args_example.rb

Aug 15th, 2013
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.43 KB | None | 0 0
  1. # using 'keyword style' arguments
  2. def mymethod(required_arg, args={})
  3.   do_fancy_stuff if args[:fancy]
  4. end
  5.  
  6. mymethod "foo",:fancy => true # => args={:fancy => true}
  7. mymethod "foo"                # => args={}
  8.  
  9. # using * (splat) arguments
  10. def mymethod(required_arg, *args)
  11.   # args is an array of extra args, maybe empty
  12. end
  13.  
  14. mymethod "foo","bar",:fancy => true # => args=["bar",{:fancy=>true}]
  15. mymethod "foo"                      # => args=[]
Add Comment
Please, Sign In to add comment