Given a method:
def x(*a)
Can calling it like so:
x('foo', y: 'bar', z: 'baz')
Ever differ from:
@yz = { y: 'bar', z: 'baz' }
x('foo', @yz)
I’m asking because I’ve found the I18n.translate method, defined as:
def translate(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
key = args.shift
backend = config.backend
locale = options.delete(:locale) || config.locale
handling = options.delete(:throw) && :throw || options.delete(:raise) && :raise # TODO deprecate :raise
raise I18n::ArgumentError if key.is_a?(String) && key.empty?
result = catch(:exception) do
if key.is_a?(Array)
key.map { |k| backend.translate(locale, k, options) }
else
backend.translate(locale, key, options)
end
end
result.is_a?(MissingTranslation) ? handle_exception(handling, result, locale, key, options) : result
end
…behaves differently when called using the different approaches above. When passing in a variable instead of the inline hash, then locale is always set to config.locale and the built-in locale fallbacks mechanism is prevented from running (regardless of whether :locale is present in the hash variable.) Yet, in IRB tests, I can’t find a way for #x to tell the difference.
I’ve worked around this for the moment by copy-pasting the hash contents into each #translate call in the app affected, but sure would by nice to know what’s up here.
no, they are identical:
Please note that the syntax you’re using will only work in Ruby 1.9 , not in 1.8