From: Rein Henrichs Date: 2010-06-18T12:25:29+09:00 Subject: Re: method interface - hash argument vs. named parameters On 2010-06-17 07:24:47 -0700, Intransition said: > I've never run into this issue before, but I'm working on a method > interface that is quite 'dense', and I need to be able to > differentiate between a hash passed to a method and named parameters. > i.e. All of these are valid: > > foo(hash, :opt=>val) > foo(hash) > foo(:opt=>val) > > However, there seems to be no way to distinguish that last the two > forms. > > Is there any way? Or am I stuck? If both the second and third option are valid then there is no way to distinguish between the two hashes. If you could narrow it down to one of them, you would be ok. If the hash is optional but the options aren't, you can use: def foo(hash_or_options, options=nil) hash = options ? hash_or_options : nil options = hash_or_options unless options ... end Alternatively, you can pass the original argument as part of the options hash foo(:hash => hash, ...) I assume you're using a more meaningful variable name than 'hash' in the real code, so this may suffice. I would not recommend mixing the two styles in the same method though (allowing a hash argument OR a :hash option). -- Rein Henrichs http://puppetlabs.com http://reinh.com