In JavaScript ES6 we can create objects where variable names become keys like this:
> let a = \'aaa\'
\'aaa\'
> let b = \'bbb\'
\'bbb\'
> { a, b }
Not built in to the language. But what do you think of this?
https://gist.github.com/smtlaissezfaire/e81356c390ae7c7d38d435ead1ce58d2
def hash_shorthand(source_binding, *symbols)
hash = {}
symbols.each do |symbol|
hash[symbol] = source_binding.local_variable_get(symbol)
end
hash
end
$ irb -r './hash_shorthand.rb'
>> x = 10
>> y = 20
>>
>> puts hash_shorthand(binding, :x, :y)
{:x=>10, :y=>20}
Only downside is that you'll need to pass the binding to get access to the local variables.