Self descriptive non-working code:
mr_hash = {alpha: "hello", bravo: self.alpha + " world"} # Not working...
should give
{alpha: "hello", bravo: "hello world"}
Is it possible, and if so how, to do it within the hash? Without using intermediate variables like:
charlie = "hello"
delta = charlie + " world"
mr_hash = {alpha: charlie, bravo: delta}
Probably not what you're looking for but you can use Object#tap to avoid creating an additional variable:
mr_hash = Hash.new.tap do |h|
h['alpha'] = 'Hello'
h['bravo'] = h['alpha'] + ' world'
end
mr_hash # => {"alpha"=>"Hello", "bravo"=>"Hello world"}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With