You can use the built-in ruby tap:
Hash.new.tap do |hash| hash.merge! a: 1 end
It can even be an “abuse” for several objects:
[one_long_name, another_long_name].tap do |(a,b)| a.prop = b.prop end
Of course, both do not give you exactly what with will do according to your example: the block will not be evaluated on the object instance. But I prefer to use tap with multiple objects, plus tap return self , so it can be bound:
[one_long_name, another_long_name].tap {|(a,b)| a.prop = b.prop }.inspect
hurikhan77
source share