iHiD
2012-May-17 17:40 UTC
Preferred method of dealing with options in final argument hash
Hi guys, I''m wondering what the preferred method of parsing the "options" hash is? In one file, I''ve just seen these three different methods: def foobar(options = {}) options.reverse_merge!(:length => 30) #... call_another_method(options[:length]) end def foobar(options = {}) options[:length] ||= 30 #... call_another_method(options[:length]) end def foobar(options = {}) length = options[:length] || 30 #... call_another_method(length) end I''d like to go through and standardise them, at least in the files I''m working on. Which is the preferred method? Thanks, Jeremy -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/O10fcEudgi4J. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Donald Ball
2012-May-17 17:48 UTC
Re: Preferred method of dealing with options in final argument hash
I would strongly advocate using the Hash#fetch method in general: options.fetch(:length, 30) Though it''s worth noting it gives different results than the || idiom for nil and false hash values. -- donald -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Jeremy Evans
2012-May-17 18:09 UTC
Re: Preferred method of dealing with options in final argument hash
On Thu, May 17, 2012 at 10:48 AM, Donald Ball <donald.ball@gmail.com> wrote:> I would strongly advocate using the Hash#fetch method in general: > > options.fetch(:length, 30) > > Though it''s worth noting it gives different results than the || idiom > for nil and false hash values.It also gives different results if options has a default value or a default proc. If the default value is expensive to produce, using the block version of Hash#fetch may be better for performance, as then it will only produce the expensive value if the options hash doesn''t contain the option. It''s generally considered bad style to modify arguments passed into a method unless that is the purpose of the method itself. Using: options.reverse_merge!(:length => 30) or options[:length] ||= 30 modifies the options hash, and that''s a bad idea IMO. Jeremy -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.